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:
Index returns

Subject: Index returns

From: Aidy

Date: 18 Oct, 2010 04:18:04

Message: 1 of 3

Hello everyone,

I have 2 matrices J and K . Where J is columns 2 and 3 of matrix K. The first column of K represent indices of the rows of K.

J =

     1 4
     1 7
     2 4
     2 5
     2 6
     4 5
     4 6
     5 7

K =

     1 1 4
     2 1 7
     3 2 3
     4 2 4
     5 2 5
     6 2 6
     7 3 4
     8 4 5
     9 4 6
    10 5 7

How can I identify the indices of J based on their existence in K . The indices I'd like to get is:


return =
                1
                2
                4
                5
                6
                8
                9
               10

thank you
aiden

Subject: Index returns

From: Nasser M. Abbasi

Date: 18 Oct, 2010 04:53:48

Message: 2 of 3

On 10/17/2010 9:18 PM, Aidy wrote:
> Hello everyone,
>
> I have 2 matrices J and K . Where J is columns 2 and 3 of matrix K. The first column of K represent indices of the rows of K.
>

wondering Why do you need to stores the 'indices' for? isn't that just
1:size(k,1) ?


> J =
>
> 1 4
> 1 7
> 2 4
> 2 5
> 2 6
> 4 5
> 4 6
> 5 7
>
> K =
>
> 1 1 4
> 2 1 7
> 3 2 3
> 4 2 4
> 5 2 5
> 6 2 6
> 7 3 4
> 8 4 5
> 9 4 6
> 10 5 7
>
> How can I identify the indices of J based on their existence in K . The indices I'd like to get is:
>
>
> return =
> 1
> 2
> 4
> 5
> 6
> 8
> 9
> 10
>
> thank you
> aiden

1:size(k,1) ?

I must be missing something here. Not sure what now. May be the example
shown is not what is meant.

--Nasser

Subject: Index returns

From: Bruno Luong

Date: 18 Oct, 2010 07:05:05

Message: 3 of 3

 [~, loc] = ismember(J,K(:,[2 3]),'rows')

You notice that I do not use the first column of K. Matlab own indexes are sufficient, no need to put explicitly 1, 2, 3 ... on the first row. It's just waste the space without adding useful information.

If your matrix K contains the last two columns then the command is simpler:

 [~, loc] = ismember(J,K,'rows')

Not only the first column is not useful, but it just makes thing more complex.

Bruno

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