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
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
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
2018-7-4
回答(1 个)
Jan
2018-7-4
for i = 1:10
disp(i)
end
is equivalent to:
i = 1;
while i <= 10
disp(i)
i = i + 1;
end
3 个评论
Biswajit Jana
2018-7-4
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?
类别
在 帮助中心 和 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!