Updating for loop boundaries in MATLAB

4 Ansichten (letzte 30 Tage)
center
center am 27 Mai 2020
Kommentiert: center am 30 Mai 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

Akzeptierte Antwort

Matt J
Matt J am 27 Mai 2020
for i = 1:size(A,1)
  5 Kommentare
Matt J
Matt J am 29 Mai 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,:);
center
center am 30 Mai 2020
Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by