COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### delete cells in cell array with certain cell size

• Email
• Follow

```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
```
 0

See related articles to this posting

```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.
```
 0

```Thanks Walter,

works pretty fine!
```
 0

```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
```
 0

```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.
```
 0

```Walter Roberson <roberson@hushmail.com> wrote in message <hqkv7o\$cpm\$1@canopus.cc.umanitoba.ca>...
> 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.

This is not equivalent to Bruno's solution.  This is, and will most certainly be slower:

cellfun(@(x) size(x,1),C)

It is my experience, at least with 2007b, that calling cellfun with the built-in strings is much faster than using function handles or anonymous functions.

Also, Bruno's proposal will fail to remove column vectors, which satisfy the requirements (I think?) stated by the OP.
```
 0

```"Matt Fig" <spamanon@yahoo.com> wrote in message <hql0n0\$hqk\$1@fred.mathworks.com>...
>
>
> Also, Bruno's proposal will fail to remove column vectors, which satisfy the requirements (I think?) stated by the OP.

It is not clear to me either Matt what cell elements have to be removed:

If all vectors must be removed, then

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

should do.

Bruno
```
 0

```Walter Roberson <roberson@hushmail.com> wrote in message <hqkv7o\$cpm\$1@canopus.cc.umanitoba.ca>...
> but hints that
>
> C(Cellfun(@size, C, 1)==1) = [];
>
> might be faster than with 'size' --

You are right Walter, this command throw out right away an syntax error, so it's certainly faster. ;-) More seriously, here is some timings:

>> clear c
>> c(1:1e6)={rand(12,2)};
>> tic; b=cellfun(@(c) size(c,1) == 1,c); toc
Elapsed time is 5.330068 seconds.
>> tic; b=cellfun('size',c,1)==1; toc
Elapsed time is 0.019914 seconds.

We have discussed about this topic not long ago. A limited number of string function handles are specifically implemented in the fast way under the hood within cellfun.

Bruno
```
 0

```"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
>
> We have discussed about this topic not long ago. A limited number of string function handles are specifically implemented in the fast way under the hood within cellfun.

Too bad too... I wish TMW would add more of these ;-).
And add some built-ins to BSXFUN, ACCUMARRAY, etc,.....
```
 0