![]() ![]() Let's first assume you have a function that you want to apply to each element of A (called my_func). There are also a couple of functions you can use: arrayfun and cellfun. (Though I don't use a 64 bit MATLAB release, I believe that problem has been resolved for those lucky individuals who do.)Īs pointed out in a few other answers, you can iterate over all elements in a matrix A (of any dimension) using a linear index from 1 to numel(A) in a single for loop. It is really only an issue if you use sparse matrices often, when occasionally this will cause a problem. So if your array has more then a total of 2^32 elements in it, the linear index will fail. MATLAB uses a 32 bit integer to store these indexes. The only problem with the linear index is when they get too large. So you can use it on structures, cell arrays, etc. The linear index applies in general to any array in matlab. Conversion between the linear index and two (or higher) dimensional subscripts is accomplished with the sub2ind and ind2sub functions. There are many circumstances where the linear index is more useful. For example, if we wanted to square the elements of A (yes, I know there are better ways to do this), one might do this: B = zeros(size(A)) The result is, we can access each element in turn of a general n-d array using a single loop. In fact, the function find returns its results as a linear index. A(:)Īs you can see, the 8th element is the number 7. We can see the order the elements are stored in memory by unrolling the array into a vector. MATLAB allows you to use either a row and column index, or a single linear index. An array in MATLAB is really just a vector of elements, strung out in memory. Note that in the latter two cases is a one-dimensional vector, and should be reshaped back into a matrix if necessary (for example, using reshape).The idea of a linear index for arrays in matlab is an important one. For instance, if we have: A = Īnd we want to extract A(, ) using logical indexing, we can do either this: Ir = logical() The subscript vector must be either of the same dimensions as the original matrix or a vector with the same number of elements. In logical indexing the subscripts are binary, where a logical 1 indicates that the corresponding element is selected, and 0 means it is not. For example, if you want to convert the subscripts in matrix A (corresponding to element 30) into a linear index, you can write sub2ind(size(A), 1, 3) (the result in this case should be 7, of course). The resulting matrix is, however always of the same dimensions as the subscript matrix.įor instance, if I =, then A(I) is the same as writing reshape(A(I(:)), size(I)).Ĭonverting from matrix subscripts to linear indices and vice versa:įor that you have sub2ind and ind2sub, respectively. The subscript matrix is simply converted into a column vector, and used for linear indexing. It is also possible to use another matrix for linear indexing. For that reason, A(:) converts any matrix A into a column vector. The special colon and end subscripts are also allowed, of course. The equivalent column vector is: A = [10 ![]() For instance, we have: A = Īnd we want to compute b = A(2). Linear indexing treats any matrix as if it were a column vector by concatenating the columns into one column vector and assigning indices to the elements respectively. This is especially useful for large matrices. The colon is just a short-hand notation for "1:end".įor example, instead of writing A(, ), you can write A(:, 2:end). ![]() end simply indicates the last index in that dimension.There are also two special subscripts: end and the colon ( :): %# Extract the first and third rows, and the first and second columnsī = A(, ) %# B = ![]() Indexing vectors can be specified for each dimension separately, for instance: A = %# Extracts the third and the ninth element They can either contain a single index or several, like so: A = Indexing vectors indicate the indices of the element to be extracted. There's an interesting article in the official documentation that comprehensively explains indexing in MATLAB.īasically, there are several ways to extract a subset of values, I'll summarize them for you: 1. The simplest way to extract the desired sub-matrix would be with an index vector: B = A(, ) As for your question, suppose you have an arbitrary 10-by-10 matrix A. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |