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!