Why are some cells not removed in an array?
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
Hello,
I need to remove some cells from an array to keep every cells of the same size. I have used this loop for the moment:
iter=1;
while iter<size(DL,2)
if cellfun(@(x) numel(x), DL(:,iter))~=41
DL(:,iter)= [];
indexsuppr=indexsuppr+1; %nb of deleted columns
end
iter=iter+1;
end
DL is a 1X47 cell array at the beginning. Some cells are 1X41 size, some are 1X24. I want to keep only the ones of 1X41. For a reason I don't understand, some 1X24 are deleted but some stay at the end of the loop (from the 24th iteration). Has somebody an idea why? thx in advance!
1 commentaire
Adam
le 12 Avr 2017
Never delete elements of an array while iterating forward through it. You are pulling the rug out from under your own feet.
Iterate backwards for deletion or, better, just find the indices of the elements to delete and then delete them all in one simple instruction rather than doing the deletion within a loop.
Réponses (1)
Nelly Moulin
le 12 Avr 2017
0 votes
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!