How do I declare a for loop with a given number of elements?
1 回表示 (過去 30 日間)
古いコメントを表示
Ana Carolina da Silva Pacheco
2021 年 5 月 15 日
コメント済み: Ana Carolina da Silva Pacheco
2021 年 5 月 16 日
I have a for loop:
for j=0:0.03
..
end
I want j to run through 100 elements, in ascending order, between 0 and 0.03 (the value 0.03 is hypothetical). Can somebody help me, please?
0 件のコメント
採用された回答
John D'Errico
2021 年 5 月 15 日
編集済み: John D'Errico
2021 年 5 月 15 日
I'll only have a loop 5 elements long, as I'm feeling tired right now. :)
jvals = linspace(0,0.03,5);
for j = jvals
disp(j)
end
You should get the idea how to change it to 100.
Do NOT use j as a matrix index, since MATLAB does not allow non-integer indexes.
If you want a vector index also, then do this:
jvals = linspace(0,0.03,5);
for j = 1:numel(jvals)
disp([j,jvals(j)])
end
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!