Updating for loop boundaries in MATLAB

4 views (last 30 days)
center
center on 27 May 2020
Commented: center on 30 May 2020
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

Accepted Answer

Matt J
Matt J on 27 May 2020
for i = 1:size(A,1)
  5 Comments
Matt J
Matt J on 29 May 2020
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,:);

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!