Info
此问题已关闭。 请重新打开它进行编辑或回答。
Why are some cells not removed in an array?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Adam
2017-4-12
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.
回答(1 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!