Updating for loop boundaries in MATLAB

1 visualizzazione (ultimi 30 giorni)
center
center il 27 Mag 2020
Commentato: center il 30 Mag 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

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by