Thread Subject: find to search cell array

Subject: find to search cell array

From: A Ram

Date: 14 Nov, 2009 13:43:01

Message: 1 of 6

Hi,

I have a cell array made up of empty cells and 1s. I used the following to get the locations of the 1s:

s2=find(s1);

but got the following error,

??? Undefined function or method 'find' for input arguments of
type 'cell'.

Thanks ! :)

Subject: find to search cell array

From: Marco Ho

Date: 15 Nov, 2009 11:24:45

Message: 2 of 6

as far as I concern,the input in find function is not allowed to be in cell type

Subject: find to search cell array

From: Oleg Komarov

Date: 15 Nov, 2009 14:05:19

Message: 3 of 6

"A Ram" <shal.hat@gmail.com> wrote in message <hdmc55$h7g$1@fred.mathworks.com>...
> Hi,
>
> I have a cell array made up of empty cells and 1s. I used the following to get the locations of the 1s:
>
> s2=find(s1);
>
> but got the following error,
>
> ??? Undefined function or method 'find' for input arguments of
> type 'cell'.
>
> Thanks ! :)
help cellfun
IDX = find(cellfun(@(x) x==1,s1))

Subject: find to search cell array

From: Jan Simon

Date: 15 Nov, 2009 15:07:02

Message: 4 of 6

Dear A Ram!

> > I have a cell array made up of empty cells and 1s. I used the following to get the locations of the 1s:
> >
> > s2=find(s1);
> > ??? Undefined function or method 'find' for input arguments of
> > type 'cell'.
> >

> help cellfun
> IDX = find(cellfun(@(x) x==1, s1))

Oleg's method fails, doesn't it? I cannot try it (Matlab 6.5...).
The == operator does not work as expected if the cell element is empty. ISEQUAL does:
  IDX = find(cellfun(@(x) isequal(x, 1), s1))

To find all not-empty cell elements:
  IDX = find(~cellfun('isempty', s1));

Good luck, Jan

Subject: find to search cell array

From: Matt

Date: 15 Nov, 2009 15:13:01

Message: 5 of 6

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hdp5em$1md$1@fred.mathworks.com>...

> The == operator does not work as expected if the cell element is empty. ISEQUAL does:
> IDX = find(cellfun(@(x) isequal(x, 1), s1))
>
> To find all not-empty cell elements:
> IDX = find(~cellfun('isempty', s1));


Just as a footnote, cellfun has been observed to run faster for 'isempty' and other select functions (mentioned in "help cellfun") than for anonymous functions like
(@(x) isequal(x, 1)

Subject: find to search cell array

From: Oleg Komarov

Date: 16 Nov, 2009 10:23:01

Message: 6 of 6

> Oleg's method fails, doesn't it? I cannot try it (Matlab 6.5...).
> The == operator does not work as expected if the cell element is empty. ISEQUAL does:
> IDX = find(cellfun(@(x) isequal(x, 1), s1))
>
> To find all not-empty cell elements:
> IDX = find(~cellfun('isempty', s1));
>
> Good luck, Jan

 Jan is right. Sry for the wrong tip.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
find A Ram 14 Nov, 2009 08:44:06
cell A Ram 14 Nov, 2009 08:44:06
1 A Ram 14 Nov, 2009 08:44:06
search A Ram 14 Nov, 2009 08:44:06
rssFeed for this Thread

Contact us at files@mathworks.com