From: Stephan on
Hi there,

how can I delete cells (arrays of different length) with a size==1 in a cell array?

example for an cell array C. Cells 3 and 6 have a size of 1x2 and have to be deleted:
>>C(1:6)
ans =
[13x2 single] [14x2 single] [1x2 single] [9x2 single] [11x2 single] [1x2 single]

I could manage it for empty cells using cellfun
% Empty cells in cell arrray will be deleted.
C(cellfun(@(C) isempty(C),C))=[];

Thanks Stephan
From: Walter Roberson on
Stephan wrote:
> Hi there,
>
> how can I delete cells (arrays of different length) with a size==1 in a
> cell array?
>
> example for an cell array C. Cells 3 and 6 have a size of 1x2 and have
> to be deleted:
>>> C(1:6)
> ans = [13x2 single] [14x2 single] [1x2 single] [9x2
> single] [11x2 single] [1x2 single]
> I could manage it for empty cells using cellfun
> % Empty cells in cell arrray will be deleted.
> C(cellfun(@(C) isempty(C),C))=[];

C(cellfun(@(c) size(c,1) == 1, C)) = [];

or

C = C(cellfun(@(c) size(c,1) > 1, C));


Note: deleting entries takes about 4 times as long as the approach of copying
the ones to be preserved.
From: Stephan on
Thanks Walter,

works pretty fine!
From: Bruno Luong on
If speed is a concern,

C(cellfun('size',C,1)==1) = [];

is significantly faster than Walter's solution

C(cellfun(@(c) size(c,1) == 1, C)) = [];

Bruno
From: Walter Roberson on
Bruno Luong wrote:
> If speed is a concern,
>
> C(cellfun('size',C,1)==1) = [];
>
> is significantly faster than Walter's solution
>
> C(cellfun(@(c) size(c,1) == 1, C)) = [];

That makes sense, Bruno, but hints that

C(Cellfun(@size, C, 1)==1) = [];

might be faster than with 'size' -- though you might need to be in a function
to detect the difference.