Skip to content
MathWorks - Mobile View
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
MathWorks
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out

Videos and Webinars

  • MathWorks
  • Videos
  • Videos Home
  • Search
  • Videos Home
  • Search
  • Contact sales
  • Trial software
5:09 Video length is 5:09.
  • Description
  • Full Transcript
  • Related Resources

How to Make a Matrix in a Loop in MATLAB

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.

Related Products

  • MATLAB

Bridging Wireless Communications Design and Testing with MATLAB

Read white paper

Feedback

Featured Product

MATLAB

  • Request Trial
  • Get Pricing

Up Next:

31:29
MATLAB: Tips and Tricks

Related Videos:

3:03
How to Store Data in a Matrix
1:21
Reshape a Matrix Without Using a for Loop
33:13
Building a Matrix in a For Loop
4:38
How to Do a Matrix Reshape by 'blocks'

View more related videos

MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (简体中文)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • Contact sales
  • Trial software

MathWorks

Accelerating the pace of engineering and science

MathWorks is the leading developer of mathematical computing software for engineers and scientists.

Discover…

Explore Products

  • MATLAB
  • Simulink
  • Student Software
  • Hardware Support
  • File Exchange

Try or Buy

  • Downloads
  • Trial Software
  • Contact Sales
  • Pricing and Licensing
  • How to Buy

Learn to Use

  • Documentation
  • Tutorials
  • Examples
  • Videos and Webinars
  • Training

Get Support

  • Installation Help
  • MATLAB Answers
  • Consulting
  • License Center
  • Contact Support

About MathWorks

  • Careers
  • Newsroom
  • Social Mission
  • Customer Stories
  • About MathWorks
  • Select a Web Site United States
  • Trust Center
  • Trademarks
  • Privacy Policy
  • Preventing Piracy
  • Application Status

© 1994-2022 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Instagram
  • YouTube
  • LinkedIn
  • RSS

Join the conversation