*
Pitambar Dayal, MathWorks
*

Learn how you can create a matrix that has an underlying pattern in a for loop using MATLAB^{®}, as well as how to use preallocation for the same process.

Hello, and welcome back to another MATLAB video. Today, we're going to talk about creating a matrix in a loop. We're going to do this by answering a few questions.

So here's question 1. Let's say I have a vector 1, 3, 6, 8, 9. And I want to make the following matrix from it, this one right here. How do I do it?

If we look closely at this matrix, we can see a pattern. The values in each row are equal to 1 plus the values above it. See, this first column reads 1, 2, 3, 4, 5. And the second column reads 3, 4, 5, 6, 7, et cetera, et cetera.

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. I'll write a is equal to 1, 3, 6, 8, 9. I'm not going to suppress the outputs in this example. And I'll show you why at the end of the video.

So now that we have our vector, we need to think about creating our matrix in a loop. We know that we need to append four rows to our current vector. So this means we're going to need four iterations in the loop. I'll go ahead and create an index variable for i equals 2 through 5. 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.

So let's walk through the statement. We enter the loop and i is equal to 2. A of I comma colon means we are indexing into all of the columns in row I, which in this case is row 2. 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. So after one iteration, A is now a 2 by 5 matrix.

We go back to the top of the loop. And this time, I equals 3. We index into all of the columns in the third row and set those values equal to 1 plus the row above it. The result will be that A is a 3-by-5 matrix. And we'll continue to iterate through the loop until we've gone through all the index variables.

Now, let's see what happens when we hit Run. As we can see, we get the matrix we were expecting. And since we left the statement unsuppressed, we can see each iteration as well.

Let's do a slightly different example. This time, we'll start with this column vector. And we want to produce the following matrix. How do we go about doing this?

So the first step is to figure out the pattern. In this case, each column's values are double the values in the column before it. So just like before, let's create our initial vector, B. And this time, we're appending three more columns to B.

So when we set up our loop, we'll say, for I equals 2:4. Next up is writing the pattern in MATLAB code. So I'll write B of colon comma I equals 2 times B of colon comma I minus 1 and then end. So if we run this, we should get the matrix we expect. And we do. Now, I just want to mention that the solution that I'm providing is non-unique. You may come up with something slightly different that still works.

So now, some of you may point out that this process would be faster if we preallocated memory. And you guys would be correct. Preallocation is a way to optimize your MATLAB code by explicitly defining the final size of a growing array or a growing matrix. It might not affect the performance of our current example. Because the matrix is really small. But it is noticeable for matrices that grow really large in size.

Let's use preallocation with the first example. Our code is going to look really similar to before, with the exception of two lines. This first line will define how large the final matrix will be. In this case, it's a 5-by-5. So I'll create a placeholder matrix of zeros that is this size.

The rest of my code is almost identical. I'll go ahead and copy it over from the first example and make one little tweak. 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. But that's it. 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.

So let's flip back to the first question and take a look at how our matrix is getting larger and larger here. To test your understanding of this, see if you can apply the same process to the second question we did. Anyway, thank you guys for tuning in. And I'll see you guys in another video.