Removing Elements from Cell Array
2 次查看(过去 30 天)
显示 更早的评论
Hello, I have a cell array with some differently sized arrays of numbers in some code . This cell array gets Modified in a loop , so the first iteration looks like this :-
a{1}=[6 7];
a{2}=[4 7];
a{3}=[1 9];
a{4}=[1 19];
a{5}=[7 23];
There are some cells which need to disappear. for eg in the next iteration of the loop I want indexes {4} and {5} to go and the cell dimension reduced.
So it becomes this way
a{1}=[6 7];
a{2}=[4 7];
a{3}=[1 9];
Assume that indexes 4 and 5 need to go, I store it in an array temp
temp = [ 4 5];
Then I wrote this code:-
for i=1:size(temp,2)
a{temp(:,i)}={};
end
This is not producing the desired result ,as the cell a still records {4} and {5} as {}. I want the cell to shrink, like how it is done for rectangular arrays if we give command a(4,:)=[] and a(5,:)=[] , the array a reduces in size.
Is it possible to do that for a cell ??
0 个评论
回答(2 个)
Azzi Abdelmalek
2013-3-4
编辑:Azzi Abdelmalek
2013-3-4
a={[6 7],[4 7],[1 9],[1 19],[7 23]};
temp = [ 4 5];
a(temp)=[]
1 个评论
Jan
2013-3-4
@Ajay: Please note, that Azzi does this in a vectorized form. When you use a FOR loop, removing the 4th cell element will move all subsequent elements to the front, such that removing the cell elements 4 and 5 requires:
a(4) = [];
a(4) = []; % Not a(5)!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!