Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Why are some cells not removed in an array?

1 visualizzazione (ultimi 30 giorni)
Nelly Moulin
Nelly Moulin il 12 Apr 2017
Chiuso: MATLAB Answer Bot il 20 Ago 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 Commento
Adam
Adam il 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.

Risposte (1)

Nelly Moulin
Nelly Moulin il 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 :)

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by