Matrix multiplication involves the action of multiplying each row vector of one matrix by each column vector of another matrix. And I'll see you guys in another video.X = T To test your understanding of this, see if you can apply the same process to the second question we did. So let's flip back to the first question and take a look at how our matrix is getting larger and larger here. Now, if we hit Run, we'll see that instead of having A grow in size, it's just overwriting the rows that already exist. Up here, we have to index into the first row of the zero matrix and overwrite these values with the values contained in this vector. I'll go ahead and copy it over from the first example and make one little tweak. So I'll create a placeholder matrix of zeros that is this size. This first line will define how large the final matrix will be. Our code is going to look really similar to before, with the exception of two lines. Let's use preallocation with the first example. But it is noticeable for matrices that grow really large in size. It might not affect the performance of our current example. Preallocation is a way to optimize your MATLAB code by explicitly defining the final size of a growing array or a growing matrix. So now, some of you may point out that this process would be faster if we preallocated memory. You may come up with something slightly different that still works. Now, I just want to mention that the solution that I'm providing is non-unique. So if we run this, we should get the matrix we expect. So I'll write B of colon comma I equals 2 times B of colon comma I minus 1 and then end. Next up is writing the pattern in MATLAB code. So when we set up our loop, we'll say, for I equals 2:4. And this time, we're appending three more columns to B. So just like before, let's create our initial vector, B. In this case, each column's values are double the values in the column before it. So the first step is to figure out the pattern. And we want to produce the following matrix. This time, we'll start with this column vector. And since we left the statement unsuppressed, we can see each iteration as well. As we can see, we get the matrix we were expecting. Now, let's see what happens when we hit Run. And we'll continue to iterate through the loop until we've gone through all the index variables. matrix containing the indices indices bsxfun (plus, (0:L-1), (0:K-1)) + 1 L-by-K matrix now get the data. The result will be that A is a 3-by-5 matrix. You can build (once for all) a matrix with the indices (1:L on first column, 1:K on first row, etc), then simply do a selection from the data vector based on those indices. We index into all of the columns in the third row and set those values equal to 1 plus the row above it. So after one iteration, A is now a 2 by 5 matrix. And the second part, equals A of I minus 1 comma colon plus 1 means that we are setting the second row equal to 1 plus the values in the row before it, in this case row 1, and then end. A of I comma colon means we are indexing into all of the columns in row I, which in this case is row 2. Then inside the loop, I'll write the following command: A of I comma colon equals A of I minus 1 comma colon plus 1, and then end. I'll go ahead and create an index variable for i equals 2 through 5. So this means we're going to need four iterations in the loop. 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. Check that both index versions refer to the same element. Create an array, and find the linear index corresponding to the element in the (2,1,2) position. 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. Convert a subscript index of a 3-D array to a single linear index. And the second column reads 3, 4, 5, 6, 7, et cetera, et cetera. See, this first column reads 1, 2, 3, 4, 5. if i have a A 1 2 5 3 4 7 how can i convert it to row B 1 2 5 3 4 7. The values in each row are equal to 1 plus the values above it. Learn more about convert, function, cell, array. 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. Hello, and welcome back to another MATLAB video.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |