- Pre-allocate Y1 and Y2
- Assign to elements of Y1 and Y2
- Do not change the loop counter, ii, in the loop
Storing data from a loop
조회 수: 4 (최근 30일)
이전 댓글 표시
I am struggling to store data from a loop, i need the data so that i am able to plot it. Thanks in advance!
for i=2:50
i=find((r>=t2n)&(r<t2p));
Y1=y(i);
Y2=mean(Y1)
end
댓글 수: 0
답변 (1개)
per isakson
2020년 5월 21일
편집: per isakson
2020년 5월 21일
The documentation says: Avoid assigning a value to the index variable within the loop statements. The for statement overrides any changes made to index within the loop.
Y1 = nan(1,50);
Y2 = nan(1,50);
for ii=2:50
ii=find((r>=t2n)&(r<t2p));
Y1(ii)=y(ii);
Y2(ii)=mean(Y1(ii))
end
댓글 수: 0
참고 항목
카테고리
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!