For loop for matrix with increasing numbers

1 回表示 (過去 30 日間)
laura9510
laura9510 2016 年 3 月 7 日
編集済み: laura9510 2016 年 3 月 7 日
I'm trying to use a for loop to create a 7x8 matrix that increases by 4 for each number (starting at 1).
So far I have something that goes
for
k = 1:8
a(k)=4*k;
end
just to create the first row... how would I go about adding more rows that keep adding increasing elements?

採用された回答

Image Analyst
Image Analyst 2016 年 3 月 7 日
Try this:
theNumber = 1;
for col = 1 : 8
for row = 1 : 7
a(row, col) = theNumber;
theNumber = theNumber + 4;
end
end
  1 件のコメント
laura9510
laura9510 2016 年 3 月 7 日
編集済み: laura9510 2016 年 3 月 7 日
I was going for increasing in the rows so I just switched the row/column. i.e.
theNumber = 1;
for row = 1:7
for col = 1:8
a(row, col) = theNumber;
theNumber = theNumber + 4;
end
end
Thanks for your help!! :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by