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:
getting index returns that are repeated

Subject: getting index returns that are repeated

From: Aidy

Date: 6 Jan, 2011 05:41:04

Message: 1 of 10

Hello guys,

I have the following 2 matrices A and B:

A =

        3 4
        7 8
        22 44
        7 8
       99 34


B =

     7 8
     7 8
     3 4


I want to return the indices of the rows of A based on their existence in matrix B.

So I use :

 [dummy, location] = ismember(B,A,'rows')


However, it returns :

location =
               4
               4
               1

Instead, the answer I'm trying to get is :
 
correct_answer =
                              2
                              4
                              1

Can anyone please help me out?

take care,
aidy

Subject: getting index returns that are repeated

From: Walter Roberson

Date: 6 Jan, 2011 07:38:08

Message: 2 of 10

On 05/01/11 11:41 PM, Aidy wrote:

> I have the following 2 matrices A and B:
>
> A =
>
> 3 4
> 7 8
> 22 44
> 7 8
> 99 34
>
>
> B =
>
> 7 8
> 7 8
> 3 4
>
>
> I want to return the indices of the rows of A based on their existence
> in matrix B.
>
> So I use :
>
> [dummy, location] = ismember(B,A,'rows')
>
>
> However, it returns :
>
> location = 4
> 4
> 1
>
> Instead, the answer I'm trying to get is :
>
> correct_answer = 2
> 4
> 1
>
> Can anyone please help me out?

The answer you are getting out of Matlab is correct for the question you
have asked. Your requirements are:

 > I want to return the indices of the rows of A based on their
 > existence in matrix B.

and that is exactly what you get out.

Your example demonstrates that your criteria is not "index at least as
far along as the row in A", because if that were your criteria,
returning 1 for the third result would not be allowed. Thus looking both
forward and backwards is permitted, and that being the case, there is
nothing in your criteria that would allow you to distinguish the answers
[4;4;1] from [2;4;1] from [2;2;1] from [4;2;1]. If you need a particular
one of those orders, you need to redefine your question.

While you are redefining your question, please be sure to cover the case
where a row appears more times in A than it does in B. Should the index
list "wrap around" to the beginning again, or if there are N repetitions
in B then should the N'th and all later repetitions in A return the same
index, or should the "excess" repetitions in A return a value indicating
that they were not found, or .... ?

Subject: getting index returns that are repeated

From: Husam Aldahiyat

Date: 6 Jan, 2011 07:55:23

Message: 3 of 10

find(ismember(B,A,'rows'))

Subject: getting index returns that are repeated

From: Aidy

Date: 6 Jan, 2011 08:09:04

Message: 4 of 10


To re-phrase the question :

 I'd like the order [2;4;1]. That is , only looking in a forward direction with no backwards movement.
  
My case is always such that a row always occurs the same number of times in both A and B ,i.e., I have no case of a row appearing more or less in either A or B. Thus, I don't suppose there any wrap around or "excess" cases.

Thanks a lot for taking the time to help and hope I'm a bit clearer on describing my issue.

--aidy

Subject: getting index returns that are repeated

From: Aidy

Date: 6 Jan, 2011 08:12:04

Message: 5 of 10

Husam,

not sure what this does.

thanks
--aidy

Subject: getting index returns that are repeated

From: Walter Roberson

Date: 6 Jan, 2011 08:12:14

Message: 6 of 10

On 06/01/11 2:09 AM, Aidy wrote:
>
> To re-phrase the question :
>
> I'd like the order [2;4;1]. That is , only looking in a forward
> direction with no backwards movement.

Then how can you possibly expect 1 for the final index? That requires
looking backwards.

Subject: getting index returns that are repeated

From: Aidy

Date: 6 Jan, 2011 08:17:05

Message: 7 of 10

Sorry,

There is backward and forward. Sorry about that.

--aiden

Subject: getting index returns that are repeated

From: Walter Roberson

Date: 6 Jan, 2011 12:05:14

Message: 8 of 10

On 06/01/11 2:17 AM, Aidy wrote:
> Sorry,
>
> There is backward and forward. Sorry about that.
>
> --aiden

Then quoting from what I posted before:

 >>> Your example demonstrates that your criteria is not "index at least
 >>> as far along as the row in A", because if that were your criteria,
 >>> returning 1 for the third result would not be allowed. Thus looking
 >>> both forward and backwards is permitted, and that being the case,
 >>> there is nothing in your criteria that would allow you to
 >>> distinguish the answers [4;4;1] from [2;4;1] from [2;2;1] from
 >>> [4;2;1]. If you need a particular one of those orders, you need to
 >>> redefine your question.

How about this proposal:

If this is the N'th time this particular row has occurred in A, then the
value that should be associated is the index of the N'th occurrence of
the row in B.


It's 6 in the morning here and I really need to get some sleep. I can
think of a couple of different approaches to the above, but I'm too
tired to plunk out a clean compact solution at the moment.

Subject: getting index returns that are repeated

From: Bruno Luong

Date: 6 Jan, 2011 12:13:05

Message: 9 of 10

A=[3 4;
    7 8;
    22 44;
    7 8;
    99 34]

B=[ 7 8;
    1 2;
    7 8;
    3 4]

[inB I] = ismember(A,B,'rows')
[trash is] = sort(I(inB));
loc = find(inB);
C = zeros(size(B,1),1);
C(ismember(B,A,'rows')) = loc(is)

% Bruno

Subject: getting index returns that are repeated

From: Aidy

Date: 6 Jan, 2011 21:49:07

Message: 10 of 10

hey bruno,

thanks pal

--aidy

Tags for this Thread

No tags are associated with 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