We know that we need to append four rows to our current vector. So now that we have our vector, we need to think about creating our matrix in a loop. And I'll show you why at the end of the video. I'm not going to suppress the outputs in this example. So now that we found the pattern, the question is, how do we create this matrix in a loop? Well, the first step is going to be to create the initial vector. And the second column reads 3, 4, 5, 6, 7, et cetera, et cetera. See, this first column reads 1, 2, 3, 4, 5. The values in each row are equal to 1 plus the values above it. If we look closely at this matrix, we can see a pattern. And I want to make the following matrix from it, this one right here. We're going to do this by answering a few questions. Today, we're going to talk about creating a matrix in a loop. Let me know here.Hello, and welcome back to another MATLAB video. Wondering if you have had some challenges not covered here when dealing with repeated indices. What Are Your Indexing Challenges When Handling Repeated Indices? Newvec(uniquesubs) = vec(uniquesubs) + accumarray(idxUnique, 10) I'm guessing you fully understand what's happening here now. vec(uniquevals) = vec(uniquevals) + accumarray(idxUnique, 10)įinally let me return to the initial vector from the beginning of the post. Notice 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 |