indexing in nested loop
이전 댓글 표시
Hi,
I have nested loops in a code and I create files that increased for every loop which works like that: data1 gives me the number of lines created in the loop for the para meters 'a' and 'b', data2 gives me for each lines created its orientation/angle.
for a=1:20
...
for b=1:20
if
...
data1(a,b)=data1(a,b)+1
data2(data1(a,b),b)= tan(x/y)
end
end
end
But when I try with non-successive values, it doesn't works.
for a=1:5:101
...
for b=1:20
if
...
data1(a(i),b)=data1(a(i),b)+1
data2(data1(a(i),b),b)= tan(x/y)
end
end
end
I thought I needed to use the index value (i) of 'a' but it doesn't work like that I supposed or I'm making a mistake somewhere.
Looking forward your advices.
Cheers,
n.
채택된 답변
추가 답변 (1개)
Robert Cumming
2011년 4월 13일
in your first example in each loop a is:
1 2 3 4 5 6 7 8 9 10 11 ... 20
In your second it is:
1 6 11 16 21 26 31 .... 101
what were you expecting a(i) to do? If your confused on the value of a in each loop - print it to the screen (or check in the debug)
The answer to this question will depend on what you want to do with your data variables.
카테고리
도움말 센터 및 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!