Creating 3x201 matrix from set of numbers

2 ビュー (過去 30 日間)
chesapeakedipper
chesapeakedipper 2020 年 7 月 7 日
コメント済み: chesapeakedipper 2020 年 7 月 7 日
Hi all,
I apologize in advance but I'm a newbie and don't have much experience with matlab - anyway here's what I'm trying to do.
I want to build a 201x3 matrix where every row has the same numbers, and the numbers in the columns go from 0 to 20e-10 with steps of 0.1e-10.
The matrix should look something like this:
[0 0 0
0.1 0.1 0.1
...
20 20 20]
I tried doing this by nesting three "for" loops in the following way:
for i = 0:0.1e-10:20e-10
for j = 0:0.1e-10:20e-10
for k = 0:0.1e-10:20e-10
I = [i,j,k];
end
end
end
but instead of obtaining 201x1 vectors, i, j and k are just numbers (20e-10, to be precise).
I tried looking into other commands but I've been asked to do this specifically with nested for loops.
Sorry in advance if it's a silly question - and thanks to whoever takes the time to help!

採用された回答

KLR
KLR 2020 年 7 月 7 日
Matt's answer is the most efficient. However since you mentioned you needed to do it specifically with for loops here's the code that will do it in a nested loop.. One thing I noticed in your loop was that your variable I was being overwritten by the latest value of i,j,k hence all you were seeing was the last value of i,j,k.
Matrix=zeros(201,3);
for i=1:1:200
for j=1:1:200
for k=1:1:200
I=i/10;
Matrix(i+1,:)=I;
end
end
end
  1 件のコメント
chesapeakedipper
chesapeakedipper 2020 年 7 月 7 日
thanks, this is great :)

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

その他の回答 (1 件)

Matt J
Matt J 2020 年 7 月 7 日
I=(0:0.1e-10:20e-10).' *[1,1,1];
  1 件のコメント
chesapeakedipper
chesapeakedipper 2020 年 7 月 7 日
way simpler than what i tried - thanks a lot :)

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by