And we'll continue to iterate through the loop until we've gone through all the index variables. The result will be that A is a 3-by-5 matrix. 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. 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. MATLAB actually supports n-dimensional matrices, so you can see how this can work for multiple dimensions.Hello, and welcome back to another MATLAB video. If your calculation is creating a matrix each time, you would then use a three-dimensional matrix, and so on. So each column might represent one time through your loop. This would also work if you were calculating a vector each time through the loop and wanted to store it as another column. So this is a very simple example of a technique that is used all the time in MATLAB where you will just take the results and store them in a matrix for easy manipulation and use later. Now that it's done what we can do is come in here and say Plot (y), and we can see that on the graph here. And each time we keep adding another column to this. And what we'll see by scrolling up through the Command Window here is that at first, we have Y is equal to a 1 by 1, then a 1 by 2, 1 by 3. So every time through the loop now this statement is going to read Y element 1 or 2, or 3, or 4, is going to equal to the same thing it did before. So what we can do is come in here and say I want to make Y into a vector. That isn't going to do very well if we want to plot this data. Now what if we wanted to plot those? Well, every time through this loop we have overwritten the value of Y so we lost, like for instance, 9.528 when we generated 10.857. And we can see we've gone through this loop 10 times and gotten different values of Y. I'm going to run it by hitting F5, which means save and run the current file. So I want to actually see the results of this. So we're going to just have a random number generated-somewhere between 0 and 1-and add it to the current value of I, and end. Now inside of this loop what we're going to do is say Y is equal to I plus rand. What we're going to do is say for I is equal 1 : 10, meaning that we're going to count from 1 to 10. In today's video on MATLAB basics, we're going to show how to store the results of a calculation inside of a vector, which is a special case of a matrix.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |