Updating for loop boundaries in MATLAB

4 ビュー (過去 30 日間)
center
center 2020 年 5 月 27 日
コメント済み: center 2020 年 5 月 30 日
Hey, I have a for loop inside of that loop, there is a for loop whose upper boundary is row length of a matrix A, inside this loop I am reaching and deleting rows of A within some condition. The problem is, as far as I have reduced row length of A, my for loop stops because its boundary exceeds the reduced row length of A. In first for loop, I am performing some expressions to updating my A matrix meaning that, I made rows of A deletable within the condition that I referred. My second for loop's boundary should be updated in every deleting operation to be able to delete itself in second loop, enventually. How can I solve this problem? The structure of my code is more or less like below:
for c = 1:10
; %statements mading rows of A deletable
for i = 1:size(A,1)
if %conditions
A(i,:) = []; %deleting rows of A, so its row length reduced
else
; %do nothing
end
end
end

採用された回答

Matt J
Matt J 2020 年 5 月 27 日
for i = 1:size(A,1)
  5 件のコメント
Matt J
Matt J 2020 年 5 月 29 日
Using false() reflects the assumption that no rows will be deleted, until it is established in the loop that a delete condition for certain rows is satisfied. But you could have done it in other ways, e.g.,
keep=true(size(A,1),1); %assume all rows will be kept (not deleted)
for i = 1:size(A,1)
if %conditions
keep(i)=false;
end
end
A=A(keep,:);
center
center 2020 年 5 月 30 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by