how will use nested loop
이전 댓글 표시
hello my problem is that i have 11 value for any variable suppose i=1:11 and have another variable suppose b=2:12 also has 11 values now my question is that i want to find out value for i=1 for z=i*b total 11 values and for i=2 another 11 value of z and so on how i can use nested loop
댓글 수: 3
Image Analyst
2012년 12월 18일
편집: Image Analyst
2012년 12월 18일
It's not clear what you want. Please give an example with 2 11-element arrays, a and b, and what output(s) you want.
manoj saini
2012년 12월 19일
Walter Roberson
2012년 12월 19일
Yes, and all of the Answers so far give you that.
채택된 답변
추가 답변 (3개)
Babak
2012년 12월 18일
Z = zeors(11,11);
for i=1:11
for b=2:12
z=i*b;
end
end
댓글 수: 4
Walter Roberson
2012년 12월 19일
You would need to change the assignment to be
z(i,b-1) = i * b;
manoj saini
2012년 12월 19일
manoj saini
2012년 12월 19일
Image Analyst
2012년 12월 19일
If the number you're multiplying by is a fractional number, like 0.1 or 0.11 then you'll have to separate your index from your number, like I think you originally had in your message where you had a and b instead of i and b. You could just make the loop index from 1 to 11 and then create the array index from it like arrayIndex = i / 10 and then use arrayIndex in z() or wherever.
Walter Roberson
2012년 12월 18일
z = bsxfun(@times, i(:), b);
Sean de Wolski
2012년 12월 18일
Code golf:
z=i'*b
댓글 수: 1
Walter Roberson
2012년 12월 18일
Provided "i" is not complex, which it normally is ;-)
카테고리
도움말 센터 및 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!