Skip to content
MathWorks - Mobile View
  • MathWorks アカウントへのサインインMathWorks アカウントへのサインイン
  • Access your MathWorks Account
    • マイ アカウント
    • コミュニティのプロファイル
    • ライセンスを関連付ける
    • サインアウト
  • 製品
  • ソリューション
  • アカデミア
  • サポート
  • コミュニティ
  • イベント
  • MATLAB を入手する
MathWorks
  • 製品
  • ソリューション
  • アカデミア
  • サポート
  • コミュニティ
  • イベント
  • MATLAB を入手する
  • MathWorks アカウントへのサインインMathWorks アカウントへのサインイン
  • Access your MathWorks Account
    • マイ アカウント
    • コミュニティのプロファイル
    • ライセンスを関連付ける
    • サインアウト

ビデオ・Webセミナー

  • MathWorks
  • ビデオ
  • ビデオ ホーム
  • 検索
  • ビデオ ホーム
  • 検索
  • 営業へのお問い合わせ
  • 評価版
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

3 Ways to Speed Up Model Predictive Controllers

Read white paper

A Practical Guide to Deep Learning: From Data to Deployment

Read ebook

Bridging Wireless Communications Design and Testing with MATLAB

Read white paper

Deep Learning and Traditional Machine Learning: Choosing the Right Approach

Read ebook

Hardware-in-the-Loop Testing for Power Electronics Control Design

Read white paper

Predictive Maintenance with MATLAB

Read ebook

Electric Vehicle Modeling and Simulation - Architecture to Deployment : Webinar Series

Register for Free

How much do you know about power conversion control?

Start quiz

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

  • 営業へのお問い合わせ
  • 評価版

MathWorks

Accelerating the pace of engineering and science

MathWorksはエンジニアや研究者向け数値解析ソフトウェアのリーディングカンパニーです。

ディスカバー…

製品を見る

  • MATLAB
  • Simulink
  • 学生向けソフトウェア
  • ハードウェア サポート
  • File Exchange

製品評価版の入手または製品の購入

  • ダウンロード
  • 評価版ソフトウェア
  • 営業へのお問い合わせ
  • 価格とライセンス
  • MathWorksストア

使い方を学ぶ

  • ドキュメンテーション
  • チュートリアル
  • 例
  • ビデオ・Webセミナー
  • トレーニング

サポートを受ける

  • インストールのヘルプ
  • MATLAB Answers
  • 技術コンサルティング
  • ライセンスセンター
  • サポートへのお問い合わせ

MathWorks について

  • 採用情報
  • ニュースルーム
  • 社会貢献
  • ユーザー事例
  • MathWorks について
  • Select a Web Site United States
  • トラストセンター
  • 商標
  • プライバシー ポリシー
  • 違法コピー防止
  • アプリケーション ステータス

© 1994-2022 The MathWorks, Inc.

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

MATLAB を語ろう