Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help with 'ismember'

Subject: Help with 'ismember'

From: Kevin Ellis

Date: 25 Nov, 2012 23:40:14

Message: 1 of 6

Hello,

I am having problems using the command ismember. I want to find identical entries in two cell arrays. The cell arrays look like:

A =
    [31010]
    [31070]
    [34267]
    [37090]
    [37590]
    [52160]
    [65100]

B =
    [34273]
    [34301]
    [37040]
    [37090]
    [37180]
    [37220]
    [37250]
    [37350]
    [52140]

This is just a smaller set of the data. Clearly the only shared entry between the two is [37090]. Also, the two cell arrays are have different lengths. In order to find the index of the shared entry I use the command:

[A, Index] = ismember(A,B);

However, I receive the error:

Error using cell/ismember>cellismemberlegacy (line 131)
Input A of class cell and input B of class cell must be cell arrays of strings,
unless one is a string.

I tried to then look at my data to figure out what the error is. The data I have is only a 5-digit number. Checking the class of each entry I have either a cell or double depending on whether I use {} or (). At this point, I am very confused and have tried using num2str to convert the data which does not work either.

So, I was wondering if anyone can help me solve this problem. Again, all I want to do is find the index of the entry that appears in both cell arrays and have used ismember without problems until now. Any help would be much appreciated.

Kevin

Subject: Help with 'ismember'

From: Nasser M. Abbasi

Date: 26 Nov, 2012 00:02:30

Message: 2 of 6

On 11/25/2012 5:40 PM, Kevin Ellis wrote:
> Hello,

You need to make the data used available and the code you used
to read it, or show the Matlab code used to generate the data
and show all the code used.

How else do you expect someone to reproduce what you are talking
about?

--Nasser

Subject: Help with 'ismember'

From: matt dash

Date: 26 Nov, 2012 00:25:10

Message: 3 of 6

"Kevin Ellis" wrote in message <k8ua8u$3a9$1@newscl01ah.mathworks.com>...
> Hello,
>
> I am having problems using the command ismember. I want to find identical entries in two cell arrays. The cell arrays look like:
>
> A =
> [31010]
> [31070]
> [34267]
> [37090]
> [37590]
> [52160]
> [65100]
>
> B =
> [34273]
> [34301]
> [37040]
> [37090]
> [37180]
> [37220]
> [37250]
> [37350]
> [52140]
>
> This is just a smaller set of the data. Clearly the only shared entry between the two is [37090]. Also, the two cell arrays are have different lengths. In order to find the index of the shared entry I use the command:
>
> [A, Index] = ismember(A,B);
>
> However, I receive the error:
>
> Error using cell/ismember>cellismemberlegacy (line 131)
> Input A of class cell and input B of class cell must be cell arrays of strings,
> unless one is a string.
>
> I tried to then look at my data to figure out what the error is. The data I have is only a 5-digit number. Checking the class of each entry I have either a cell or double depending on whether I use {} or (). At this point, I am very confused and have tried using num2str to convert the data which does not work either.
>
> So, I was wondering if anyone can help me solve this problem. Again, all I want to do is find the index of the entry that appears in both cell arrays and have used ismember without problems until now. Any help would be much appreciated.
>
> Kevin

You can only use ismember on cell arrays if the cell arrays contain strings. Yours contain numbers. Just use cell2mat to turn them back into regular numerical arrays before calling ismember.

Subject: Help with 'ismember'

From: dpb

Date: 26 Nov, 2012 00:30:41

Message: 4 of 6

On 11/25/2012 5:40 PM, Kevin Ellis wrote:
...
> I am having problems using the command ismember. I want to find
> identical entries in two cell arrays. The cell arrays look like:
>
> A = [31010]
> [31070]
> [34267]
...
> B = [34273]
> [34301]
> [37040]
...
>
> This is just a smaller set of the data. Clearly the only shared entry
> between the two is [37090]. Also, the two cell arrays are have different
> lengths. In order to find the index of the shared entry I use the command:
>
> [A, Index] = ismember(A,B);
>
> However, I receive the error:
>
> Error using cell/ismember>cellismemberlegacy (line 131)
> Input A of class cell and input B of class cell must be cell arrays of
> strings,
> unless one is a string.
>
> I tried to then look at my data to figure out what the error is....

The problem is you've put the values into a cell array w/ only a single
value per cell instead of either a vector or a cell of N values...

Thus, when you try to dereference the cell, you got problems...the {}
curly brackets give you a comma-separated list which the function
doesn't know how to handle.

The solution is to either recast the data into a more useful form as
noted above or force the comma list into a vector to pass...

[A, Index] = ismember([A{:}],[B{:}]);

--

Subject: Help with 'ismember'

From: Kevin Ellis

Date: 26 Nov, 2012 01:53:10

Message: 5 of 6

dpb <none@non.net> wrote in message <k8ud9a$9nv$1@speranza.aioe.org>...
> On 11/25/2012 5:40 PM, Kevin Ellis wrote:
> ...
> > I am having problems using the command ismember. I want to find
> > identical entries in two cell arrays. The cell arrays look like:
> >
> > A = [31010]
> > [31070]
> > [34267]
> ...
> > B = [34273]
> > [34301]
> > [37040]
> ...
> >
> > This is just a smaller set of the data. Clearly the only shared entry
> > between the two is [37090]. Also, the two cell arrays are have different
> > lengths. In order to find the index of the shared entry I use the command:
> >
> > [A, Index] = ismember(A,B);
> >
> > However, I receive the error:
> >
> > Error using cell/ismember>cellismemberlegacy (line 131)
> > Input A of class cell and input B of class cell must be cell arrays of
> > strings,
> > unless one is a string.
> >
> > I tried to then look at my data to figure out what the error is....
>
> The problem is you've put the values into a cell array w/ only a single
> value per cell instead of either a vector or a cell of N values...
>
> Thus, when you try to dereference the cell, you got problems...the {}
> curly brackets give you a comma-separated list which the function
> doesn't know how to handle.
>
> The solution is to either recast the data into a more useful form as
> noted above or force the comma list into a vector to pass...
>
> [A, Index] = ismember([A{:}],[B{:}]);
>
> --

Thanks dpb. That works perfectly. Also thanks for the help on how to use ismember. I didn't realize that this was a condition for using ismember.

Kevin

Subject: Help with 'ismember'

From: dpb

Date: 26 Nov, 2012 02:18:26

Message: 6 of 6

On 11/25/2012 7:53 PM, Kevin Ellis wrote:
...

> Thanks dpb. That works perfectly. Also thanks for the help on how to use
> ismember. I didn't realize that this was a condition for using ismember.
...

You misunderstand the problem, then. It isn't anything specific to
ismember(). The same problem would exist trying to call any function
expecting an array instead of a cell or comma-separated list.

Search the help files for "comma-separated list" and read up on them and
dereferencing cell arrays.

--

Tags for this Thread

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.

Contact us