exit from the cycle

2 ビュー (過去 30 日間)
Lev Mihailov
Lev Mihailov 2019 年 11 月 7 日
回答済み: darova 2019 年 11 月 7 日
Hello! If I create a cycle, I need it, when the cycle was executed, it went to the next column and saved the number of digits until it was executed
for i = 1:length(A)
if A(i,i)<A(i,i) % column and row number
B{i}=A
end
end
and I have two questions
1) how to end a cycle when conditions are fulfilled for the first time
2) how to work with rows and columns

回答 (2 件)

Bhaskar R
Bhaskar R 2019 年 11 月 7 日
1) how to end a cycle when conditions are fulfilled for the first time
Use break keyword to exit cycle as
for i = 1:length(A)
% actually "A(i,i)<A(i,i)"it always fails
if A(i,i)<threshold_value %comparing fake threshold_value % condition
B{i}=A;
break; % to break cycle
end
end
2) how to work with rows and columns
To work with rows and columns use to for loops as
for i = 1:size(A,1) % for row access
for j = 1:size(A,2) % for column access
if A(i,j) < threshold_value % condition
B{i, j}=A; % some opeartion
end
end
Refer MATLAB basic documentation help to learn MATLAB fundamentals

darova
darova 2019 年 11 月 7 日
Use break to exit loop
  • 2) how to work with rows and columns
The question is unclear. You can use 2 for loops
for i = 1:size(A,1) % rows
for j = 1:size(A,2) % columns
% if ...
end
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by