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:
ismember

Subject: ismember

From: Jerry

Date: 13 Nov, 2013 01:10:14

Message: 1 of 23

Hello,

When I ran the "ismember" function:

D= ismember(R, N);

I got this error:

Index exceeds matrix dimensions.

>> size (D)
ans =
       333 34
>> size (R)
ans =
       333 34
>> size (N)
ans =
       20 1

Thanks,
Jerry

Subject: ismember

From: Jerry

Date: 13 Nov, 2013 01:44:07

Message: 2 of 23

Sorry, I just noticed that I missed one line of the script in my previous message.

When I ran the "ismember" function:
 
D= ismember(R, N);

ON=R (D,:);

I got this error:
 
Index exceeds matrix dimensions.
 
>>size (D)
ans =
       333 34
size (R)
ans =
      333 34
>> size (N)
 ans =
        20 1
 
Thanks,
Jerry

Subject: ismember

From: Roger Stafford

Date: 13 Nov, 2013 02:21:05

Message: 3 of 23

"Jerry " <jerrycholo@gmail.com> wrote in message <l5ulh7$a14$1@newscl01ah.mathworks.com>...
> D= ismember(R, N);
> ON=R (D,:);
> I got this error:
> Index exceeds matrix dimensions.
- - - - - - -
  When you write "ON=R(D,:);" the variable D is converted into a single logical vector of length 333*34 = 11322 elements which determine which rows of R are to be copied into ON. The trouble is that R does not have that many rows - it has only 333 rows.

  Perhaps you meant something like this:

 ON = reshape(R(D),size(R));

Roger Stafford

Subject: ismember

From: Roger Stafford

Date: 13 Nov, 2013 02:25:06

Message: 4 of 23

"Roger Stafford" wrote in message <l5unmh$6b3$1@newscl01ah.mathworks.com>...
> ON = reshape(R(D),size(R));
- - - - - - -
  No, I take that last statement back. R(D) cannot in general be reshaped that way. However R(D) by itself would contain all the elements of R that were in N.

Roger Stafford

Subject: ismember

From: Jerry

Date: 13 Nov, 2013 02:36:10

Message: 5 of 23

OK, when I ran the line:

ON = reshape(R(D),size(R));

I got this error:

Error using reshape
To RESHAPE the number of elements must not change.

Thanks,
Jerry

Subject: ismember

From: Steven Lord

Date: 13 Nov, 2013 15:06:46

Message: 6 of 23


"Jerry " <jerrycholo@gmail.com> wrote in message
news:l5uoiq$ghj$1@newscl01ah.mathworks.com...
> OK, when I ran the line:
>
> ON = reshape(R(D),size(R));
>
> I got this error:
>
> Error using reshape
> To RESHAPE the number of elements must not change.

Yes. Look at the number of elements in R(D), then look at the number of
elements in R.

You can't turn a 5-element vector into a 2-by-3 matrix; you don't have
enough elements. You can't turn that vector into a 2-by-2 matrix either; you
have too many elements. The best you could do in that case would be to
create a 1-by-5 vector or a 5-by-1 vector or a 1-by-<some number of
dimensions of size 1>-by-5 array.

Going back to the original problem, which was an issue with ISMEMBER, show
the group the FULL text of the error you receive (including the line on
which the error occurred.) Also check using "which -all" (and post that
"which -all" output) that you're using the ISMEMBER function included in
MATLAB rather than a different one from some other toolbox that behaves
differently and is shadowing the one in MATLAB.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: ismember

From: Jerry

Date: 13 Nov, 2013 15:30:16

Message: 7 of 23

Hi,

Thank you for the comments. One thing that I would like to share it with you is that when I applied “ismember” for following data, it worked out. Any idea???

D= ismember(R, N);
ON=R (D,:);

>> size (D)
ans =
       336 2
>> size (R)
ans =
       336 2
>> size (N)
ans =
       20 1

Thanks,
Jerry

Subject: ismember

From: Steven Lord

Date: 13 Nov, 2013 16:09:34

Message: 8 of 23


"Jerry " <jerrycholo@gmail.com> wrote in message
news:l605u8$13$1@newscl01ah.mathworks.com...
> Hi,
>
> Thank you for the comments. One thing that I would like to share it with
> you is that when I applied ismember for following data, it worked out.
> Any idea???

Yes. As I said before: going back to the original problem, which was an
issue with ISMEMBER, show the group the FULL text of the error you receive
(including the line on which the error occurred.) Also check using
"which -all" (and post that "which -all" output) that you're using the
ISMEMBER function included in MATLAB rather than a different one from some
other toolbox that behaves differently and is shadowing the one in MATLAB.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: ismember

From: someone

Date: 13 Nov, 2013 16:23:06

Message: 9 of 23

"Jerry " <jerrycholo@gmail.com> wrote in message <l605u8$13$1@newscl01ah.mathworks.com>...
> Hi,
>
> Thank you for the comments. One thing that I would like to share it with you is that when I applied “ismember” for following data, it worked out. Any idea???
>
> D= ismember(R, N);
> ON=R (D,:);
>
> >> size (D)
> ans =
> 336 2
> >> size (R)
> ans =
> 336 2
> >> size (N)
> ans =
> 20 1
>
> Thanks,
> Jerry

% Are you sure it "workrd out?" I don't see how.
% I don't think your problem is with the ismember statement,
 % I think your problem is with the ON=R (D,:); statement.

% If D is 336X2 and R is 336X2, then
% R(D,:) doesn't make any sense.
% You are trying to use a 2D matrix to index a single row of R.
% Perhaps you simply mean:

ON=R(D);

Subject: ismember

From: Jerry

Date: 14 Nov, 2013 02:00:12

Message: 10 of 23

Hi,

Thank you for your comments.

> % Are you sure it "worked out?" I don't see how.

Yes, it is worked out, and the output (ON) is a matrix (n-by-2) what I needed.

> ON=R(D);

Yes, this is working, but the output is only a vector. It is not what I was looking for. What I want is a matrix (n-by-34).

Thanks,
Jerry

Subject: ismember

From: Jerry

Date: 14 Nov, 2013 05:56:05

Message: 11 of 23

> Going back to the original problem, which was an issue with ISMEMBER, show
> the group the FULL text of the error you receive (including the line on
> which the error occurred.)

ON=R (D,:);
Index exceeds matrix dimensions.

> Also check using "which -all" (and post that
> "which -all" output) that you're using the ISMEMBER function included in
> MATLAB rather than a different one from some other toolbox that behaves
> differently and is shadowing the one in MATLAB.

C:\Program Files\MATLAB\R2013a\toolbox\matlab\ops\ismember.m
C:\Program Files\MATLAB\R2013a\toolbox\matlab\ops\@cell\ismember.m % cell method
C:\Program Files\MATLAB\R2013a\toolbox\distcomp\gpu\@gpuArray\ismember.m % gpuArray method
C:\Program Files\MATLAB\R2013a\toolbox\mbc\mbc\@xregpointer\ismember.m % xregpointer method
C:\Program Files\MATLAB\R2013a\toolbox\mbc\mbc\@guidarray\ismember.m % guidarray method
C:\Program Files\MATLAB\R2013a\toolbox\mbc\mbcdata\@cgprojconnections\ismember.m % cgprojconnections method
C:\Program Files\MATLAB\R2013a\toolbox\shared\statslib\@ordinal\ismember.m % ordinal method
C:\Program Files\MATLAB\R2013a\toolbox\shared\statslib\@nominal\ismember.m % nominal method
C:\Program Files\MATLAB\R2013a\toolbox\shared\statslib\@dataset\ismember.m % dataset method
C:\Program Files\MATLAB\R2013a\toolbox\shared\statslib\@categorical\ismember.m % categorical method
C:\Program Files\MATLAB\R2013a\toolbox\symbolic\symbolic\@sym\ismember.m % sym method

Thanks,
Jerry

Subject: ismember

From: Steven Lord

Date: 14 Nov, 2013 14:49:06

Message: 12 of 23


"Jerry " <jerrycholo@gmail.com> wrote in message
news:l61oll$f90$1@newscl01ah.mathworks.com...
>> Going back to the original problem, which was an issue with ISMEMBER,
>> show the group the FULL text of the error you receive (including the line
>> on which the error occurred.)
>
> ON=R (D,:);
> Index exceeds matrix dimensions.

Okay. So this isn't actually a problem with ISMEMBER, it's a problem with
using the output _from_ ISMEMBER.

> D= ismember(R, N);
> ON=R (D,:);

If R and N are matrices, this will return a D logical array the same size as
R. Let's look at a smaller example.

>> R = magic(4)

R =

    16 2 3 13
     5 11 10 8
     9 7 6 12
     4 14 15 1

>> N = R([1 3], :)

N =

    16 2 3 13
     9 7 6 12

>> D = ismember(R, N)

D =

     1 1 1 1
     0 0 0 0
     1 1 1 1
     0 0 0 0

When you attempt to index into R using D this way:

x = R(D, :)

MATLAB treats D as a logical _vector_ to use to select rows from R.

>> indexVector = D(:)

indexVector =

     1
     0
     1
     0
     1
     0
     1
     0
     1
     0
     1
     0
     1
     0
     1
     0

This will attempt to select, among other things, the 15th row of R. But R
only has 4 rows! Therefore you receive the error about "Index exceeds matrix
dimensions."

You may be trying to determine _row_ membership, in which case you should
use the 'rows' flag with ISMEMBER:

>> D2 = ismember(R, N, 'rows')

D2 =

     1
     0
     1
     0

>> R2 = R(D2, :)

R2 =

    16 2 3 13
     9 7 6 12

If that's not what you're trying to do, explain IN WORDS (no code or
equations) your goal and someone may be able to explain how to achieve what
you want.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: ismember

From: Jerry

Date: 14 Nov, 2013 18:04:06

Message: 13 of 23

Thank you for all the comments. Yes, you are right; to clarify everything I generated exact data structure that I have; R is a cell (5-by-7) and N is also a cell (4-by1):

R =

    'rte' '345' '---' '---' 'rte' '345_rt' '---'
    'wer' '123' '---' '---' 'wer' '683_iu' '234_tr'
    'ytr' '476' '543' '467' 'ytr' '987_kj' '---'
    'poi' '890' '343' '---' 'poi' '873_mn' '875_jh'
    'uyt' '345' '---' '---' 'uyt' '234_io' '---'

N =

    '345'
    '476'
    '467'
    '343'

D = ismember(R, N)

D =

     0 1 0 0 0 0 0
     0 0 0 0 0 0 0
     0 1 0 1 0 0 0
     0 0 1 0 0 0 0
     0 1 0 0 0 0 0

 x = R(D, :)
Index exceeds matrix dimensions.


D2 = ismember(R, N, 'rows')
Warning: Third argument is ignored for cell arrays.

> In cell.ismember at 22

D2 =

     0 1 0 0 0 0 0
     0 0 0 0 0 0 0
     0 1 0 1 0 0 0
     0 0 1 0 0 0 0
     0 1 0 0 0 0 0

R2 = R(D2, :)
Index exceeds matrix dimensions.

Please let me know how I could get output based on these inputs using "ismember" or any other function.

Thanks,
Jerry

Subject: ismember

From: dpb

Date: 14 Nov, 2013 18:41:03

Message: 14 of 23

On 11/14/2013 12:04 PM, Jerry wrote:
...

> Please let me know how I could get output based on these inputs using
> "ismember" or any other function.

Well, you have "output"...the question which you've never explained is
what,specifically, are you looking for?

--

Subject: ismember

From: Jerry

Date: 14 Nov, 2013 18:52:05

Message: 15 of 23

You are right; based on D,

D =

     0 1 0 0 0 0 0
     0 0 0 0 0 0 0
     0 1 0 1 0 0 0
     0 0 1 0 0 0 0
     0 1 0 0 0 0 0

I would expect to get this output which is X:

X =

    'rte' '345' '---' '---' 'rte' '345_rt' '---'
    'ytr' '476' '543' '467' 'ytr' '987_kj' '---'
    'poi' '890' '343' '---' 'poi' '873_mn' '875_jh'
    'uyt' '345' '---' '---' 'uyt' '234_io' '---'

Thanks,
Jerry

Subject: ismember

From: dpb

Date: 14 Nov, 2013 19:29:01

Message: 16 of 23

On 11/14/2013 12:52 PM, Jerry wrote:
> You are right; based on D,
>
> D =
>
> 0 1 0 0 0 0 0
> 0 0 0 0 0 0 0
> 0 1 0 1 0 0 0
> 0 0 1 0 0 0 0
> 0 1 0 0 0 0 0
>
> I would expect to get this output which is X:
>
> X =
> 'rte' '345' '---' '---' 'rte' '345_rt' '---' 'ytr' '476' '543' '467'
> 'ytr' '987_kj' '---' 'poi' '890' '343' '---' 'poi' '873_mn' '875_jh'
> 'uyt' '345' '---' '---' 'uyt' '234_io' '---'

Why would you expect that? If you want X, what is the point of ismember()?

Again, what is the end result you're after?

--

Subject: ismember

From: Jerry

Date: 14 Nov, 2013 19:43:15

Message: 17 of 23

I thought "ismember" allows me to get "X" from "R" by having "N". If you think there is other solution(s) to solve this; please let me know what this solution is. I just need "X", that is it.

Thanks,
Jerry

Subject: ismember

From: Bruce Bowler

Date: 14 Nov, 2013 20:22:36

Message: 18 of 23

On Thu, 14 Nov 2013 19:43:15 +0000, Jerry wrote:

> I thought "ismember" allows me to get "X" from "R" by having "N". If
> you think there is other solution(s) to solve this; please let me know
> what this solution is. I just need "X", that is it.
>
> Thanks,
> Jerry

It's very unclear what you want, but is this close?

R =

    'rte' '345' '---' '---' 'rte' '345_rt' '---'
    'wer' '123' '---' '---' 'wer' '683_iu' '234_tr'
    'ytr' '476' '543' '467' 'ytr' '987_kj' '---'
    'poi' '890' '343' '---' 'poi' '873_mn' '875_jh'
    'uyt' '345' '---' '---' 'uyt' '234_io' '---'

N =

    '345'
    '476'
    '467'
    '343'

D = ismember(R, N)

ir = sum(D,2)

ir =

     1
     0
     2
     1
     1

x = R(ir>0,:)

x =

    'rte' '345' '---' '---' 'rte' '345_rt' '---'
    'ytr' '476' '543' '467' 'ytr' '987_kj' '---'
    'poi' '890' '343' '---' 'poi' '873_mn' '875_jh'
    'uyt' '345' '---' '---' 'uyt' '234_io' '---'

Bruce

Subject: ismember

From: dpb

Date: 14 Nov, 2013 20:28:38

Message: 19 of 23

On 11/14/2013 1:43 PM, Jerry wrote:
> I thought "ismember" allows me to get "X" from "R" by having "N". If you
> think there is other solution(s) to solve this; please let me know what
> this solution is. I just need "X", that is it.

I fail to understand at all now.

Start at the beginning w/ a verbal explanation of what you have and what
you want.

ismember(A,B) for arrays A and B returns an (logical) array of the same
   size as A containing true where the elements of A are in B and false
otherwise.

Using that returned logical array on A will result in a list of the
elements found which will be length(sum(LIA(:))) in length but only
contain values found in A which are in B.

--

Subject: ismember

From: Jerry

Date: 14 Nov, 2013 20:59:07

Message: 20 of 23

Sorry, if I had the problem to clearly explain what I have and what I want. There is a cell (5-by-7); let’s call it “R”. If the elements of “N” (4-by-1) are within “R” it is true, otherwise it is false, in that this logic was reflected in “D”; I used this “ismember(R, N)” to determine “D”. Now, I would use “D” as an index to create “X” (4-by-7).

Thanks,
Jerry

Subject: ismember

From: Bruce Bowler

Date: 14 Nov, 2013 21:05:49

Message: 21 of 23

On Thu, 14 Nov 2013 20:59:07 +0000, Jerry wrote:

> Sorry, if I had the problem to clearly explain what I have and what I
> want. There is a cell (5-by-7); let’s call it “R”. If the elements of
> “N” (4-by-1) are within “R” it is true, otherwise it is false, in that
> this logic was reflected in “D”; I used this “ismember(R, N)” to
> determine “D”. Now, I would use “D” as an index to create “X” (4-by-7).
>
> Thanks,
> Jerry

What's missing in your explanation (but is hidden in your earlier example)
is *how do you decide which of the rows gets eliminated*. It appears as
if it's any row that doesn't have any N in it, but you should have
specified that. If that's not the case, explain what your elimination
criteria are.

Bruce

Subject: ismember

From: dpb

Date: 14 Nov, 2013 21:12:11

Message: 22 of 23

On 11/14/2013 2:59 PM, Jerry wrote:
> Sorry, if I had the problem to clearly explain what I have and what I
> want. There is a cell (5-by-7); let’s call it “R”. If the elements of
> “N” (4-by-1) are within “R” it is true, otherwise it is false, in that
> this logic was reflected in “D”; I used this “ismember(R, N)” to
> determine “D”. Now, I would use “D” as an index to create “X” (4-by-7).
>

Well, that's a convoluted way to say it at best, but I _think_ you're
looking for sotoo...

 >> x=R(any(ismember(R,N),2),:)
x =
     'rte' '345' '---' '---' 'rte' '345_rt' '---'
     'ytr' '476' '543' '467' 'ytr' '987_kj' '---'
     'poi' '890' '343' '---' 'poi' '873_mn' '875_jh'
     'uyt' '345' '---' '---' 'uyt' '234_io' '---'
 >>

You've got to convert the logical array to a single logical index array
for the rows in R to use as the first indexing expressing if your
objective is to find the rows in R that contain a member of N. In
essence, the any(xxx,2) above simulates the 'rows' option in a case
where you don't really want to compare on rows but it is the row index
have a nonzero value somewhere within it that is significant (at least
if my parsing of the desire is correct).

I missed that the 'X' array wasn't the starting point in the previous
posting having come in sorta' late in the conversation.

A prime example that help/answers are only as good as the explanation of
the question...if folks can't figure out what it is that is wanted, it's
unlikely they'll get the expected result (early on anyway).

--

Subject: ismember

From: Steven Lord

Date: 14 Nov, 2013 22:06:11

Message: 23 of 23


"Jerry " <jerrycholo@gmail.com> wrote in message
news:l6364l$bq4$1@newscl01ah.mathworks.com...
> You are right; based on D,
>
> D =
>
> 0 1 0 0 0 0 0
> 0 0 0 0 0 0 0
> 0 1 0 1 0 0 0
> 0 0 1 0 0 0 0
> 0 1 0 0 0 0 0
>
> I would expect to get this output which is X:
>
> X =
>
> 'rte' '345' '---' '---' 'rte' '345_rt' '---' 'ytr' '476' '543' '467'
> 'ytr' '987_kj' '---' 'poi' '890' '343' '---' 'poi' '873_mn' '875_jh'
> 'uyt' '345' '---' '---' 'uyt' '234_io' '---'
> Thanks,
> Jerry

So you want the rows of R containing ANY of the strings in N? You were
close.


% Find those elements in R that are in N
D = ismember(R, N)

% Ask if ANY of the elements in D are true, operating along the rows
% Since D has the same number of rows as R, doesThisRowContainStringsFromN
% will have that same number of rows
doesThisRowContainStringsFromN = any(D, 2)

% Use the true values from doesThisRowContainStringsFromN to extract the
% corresponding rows of R
rowsOfRContainingAnyStringFromN = R(doesThisRowContainStringsFromN, :)


--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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