How do I remove an element from cell based on the size of the array within the cell
Show older comments
For this code, I took a row vector of doubles and separated it into a cell array based on values that are within 5 of the previous number in the array. Now what I want to do is remove any cells that only have one value in it. I have been able to transform the 1 x 1 array to a 0 x 0, but I haven't been able to remove the cell entirely.
What I did to remove the array:
for i = 1:length(ClusterCell)
if size(ClusterCell{1,i}) == [1 1]
ClusterCell{1,i} = [];
end
end
when I try ClusterCell(1, i) = [ ]; I get the error "A null assignment can have only one non-colon index."
How do I completely remove cell containing the 1 x 1 array instead of deleting to contents within?
Thank you for you Help!
Answers (2)
David Hill
on 28 Jan 2021
Edited: David Hill
on 28 Jan 2021
for i = 1:length(ClusterCell)
if size(ClusterCell{i}) == [1 1]
ClusterCell(i) = [];
end
end
You can use cellfun with numel to eliminate cells with only 1 element.
Assuming ClusterCell is a cell array,
ClusterCell(cellfun(@numel, ClusterCell)==1) = [];
If you want to remove the empty, 0x0 cells along with the 1x1 cells,
ClusterCell(cellfun(@numel, ClusterCell)<=1) = [];
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!