9/28/2023 0 Comments Vector indexing matlabNotice that I call the function unique and retrieve the third output, the actual locations of the unique indices as they appear in the output. I want to compute something like vec(subs) = vec(subs) + 10 with the difference being that I want repeated indices to accumulate the number of 10s represented by the repeated indices. This has been so handy that eventually we made an analogous function, accumarray for non-sparse arrays as well. From the doc, you can see that you can accumulate values when constructing a sparse matrix. You may now that you can create and use sparse matrices in MATLAB. But maybe you wanted to accumulate the results for repeated elements, but it's not so tidy that you can simply use something like cumsum. No extra accumulation of 10s for element 3. Head top to bottom (even for multidimensional arrays, since MATLAB stores the data in a column-major format), and it replaces element 1 with a new value, element 3 with a new value, and then does the latter one more time. Once we have finished computing the right-hand side, MATLAB works on the assignment. ![]() Remember I said that the MATLAB behavior is as if we placed the right-hand side into a temporary array. So the computed right-hand side has element 1 and 2 copies of the updated element 3 - updated each in the same way, since that's what the code says to do. However, we have repeated the element 3 index. What you see here is element 1 growing by 10 and same for element 3. Now let's see what we need to do if we have repeated indices in an assignment. Replicated Elements for Assignment to Output Please note that you often do not need any of these techniques for certain computations that can be efficiently accomplished with the older bsxfun, and, more recently, the, in my opinion elegant, implicit expansion behavior ( 1, 2) that you can use to "expand" singleton dimensions. First by indexing, threecols = vec(:,) % or vec(:, ones(1,numdups) threecols =īy matrix multiplication threecols = vec * % or vec * ones(1,numdups) threecols = Here's how you can create a matrix from replicated columns. Following that, the values in the temporary array are assigned to the output newvec. ![]() From a semantic, or meaning, point of view, MATLAB creates a new temporary array extracting the pieces of vec requested. I create my indices - and you can see that I want the first element followed by the 3rd one twice. ![]() Here's an even simpler example that I will extend further. After creating my array and identifying the location of values I wish to repeat ( oddlocs), I reshape this array into a column vector locs, and use this array, with its repeated values, to index into the rows I requested, including all the columns (but there is only 1 column here). Just in case you need some clarification, let me explain what's going on here. vec = Īs you can see, since I requested some repeated values, MATLAB returned them to me. Suppose I want to make numdups copies of the elements in odd locations in a vector, vec. What Are Your Indexing Challenges When Handling Repeated Indices?Īccessing Array Elements with Repeated Indices.Replicated Elements for Assignment to Output.Accessing Array Elements with Repeated Indices.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |