Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Why are some cells not removed in an array?

1 Ansicht (letzte 30 Tage)
Nelly Moulin
Nelly Moulin am 12 Apr. 2017
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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 Kommentar
Adam
Adam am 12 Apr. 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.

Antworten (1)

Nelly Moulin
Nelly Moulin am 12 Apr. 2017
I have finally solved my problem by storing the good cells in another array instead of deleting the wrong ones in the first array but I'm still interested by the answer for the first problem if someone has it :)

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by