Removing Elements from Cell Array
2 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Azzi Abdelmalek
on 4 Mar 2013
Edited: Azzi Abdelmalek
on 4 Mar 2013
a={[6 7],[4 7],[1 9],[1 19],[7 23]};
temp = [ 4 5];
a(temp)=[]
1 Comment
Jan
on 4 Mar 2013
@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)!
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!