Convert the for loop into while loop.

matrix=[6 3 5; 8 4 6; 2 1 9;7 5 2]
[r,c]=size(matrix);
for Row=1:r
for Col=1:c
fprintf('Element(%d,%d)=%d.\n',Row,Col,matrix(Row,Col))
end
end

댓글 수: 4

Rik
Rik 2018년 7월 4일
Why do you want to convert these for-loops into while-loops? This seems like the perfect place for a for-loop.
Also, you can format your code by selecting your code and clicking the {}Code button.
Biswajit  Jana
Biswajit Jana 2018년 7월 4일
Thanks,i know,I'm just trying to code in while to know the syntax of the given code instead of using for loop what will it be stand in while loop?/
Jan
Jan 2018년 7월 4일
편집: Jan 2018년 7월 4일
I hesitate to convert your code directly to a running version with while, because this might be a homework question.
Biswajit  Jana
Biswajit Jana 2018년 7월 4일
Nope not a homework, just trying to do same code in different loops and all,kindly help me out with while.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2018년 7월 4일

1 개 추천

for i = 1:10
disp(i)
end
is equivalent to:
i = 1;
while i <= 10
disp(i)
i = i + 1;
end

댓글 수: 3

Biswajit  Jana
Biswajit Jana 2018년 7월 4일
I know this.I want to do with matrix as given in the question.
Rik
Rik 2018년 7월 4일
This concept should be easy to apply to your case. Why don't you try something and show the result here if it fails?
Jan
Jan 2018년 7월 4일
편집: Jan 2018년 7월 4일
@Biswajit Jana: You know how to convert one for loop into a while loop. Your code has two for loops. You can simply copy my example and paste it twice inside each other. Then rename the loop counters and limits. It is not hard. Please try it.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 7월 4일

편집:

Jan
2018년 7월 4일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by