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 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. In 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 two-by-five 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 three-by-five 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 colon 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 pre-allocated memory. And you guys would be correct.
Pre-allocation 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 pre-allocation 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 five-by-five. So I'll create a placeholder matrix of 0's that is the 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 0 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 overriding 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.