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:
Compare two vectors and got indices of the identical numbers

Subject: Compare two vectors and got indices of the identical numbers

From: Danny

Date: 16 Feb, 2013 23:53:06

Message: 1 of 5

How to compare 2 vectors and got the indices of 2 identical numbers

lets say I have matrix a
>>a=[250;500;750;200;600;800;230;650;800]

a =

   250
   500
   750
   200
   600
   800
   230
   650
   800

and I sort matrix a to matrix b

>> b=sort(a)

b =

   200
   230
   250
   500
   600
   650
   750
   800
   800

and I want to compare matrix b to matrix a to identify in which indices of matrix a is equal to the content of matrix b.

For example for the number 230 in matrix b is located in the indices of #7 in matrix a
or number 650 in matrix b is located in the indices of #8 in matrix a

Thanks before for the answer

Regards

Subject: Compare two vectors and got indices of the identical numbers

From: Nasser M. Abbasi

Date: 17 Feb, 2013 00:52:37

Message: 2 of 5

On 2/16/2013 5:53 PM, Danny wrote:
> How to compare 2 vectors and got the indices of 2 identical numbers
>
> lets say I have matrix a
>>> a=[250;500;750;200;600;800;230;650;800]
>
> a =
>
> 250
> 500
> 750
> 200
> 600
> 800
> 230
> 650
> 800
>
> and I sort matrix a to matrix b
>
>>> b=sort(a)
>
> b =
>
> 200
> 230
> 250
> 500
> 600
> 650
> 750
> 800
> 800
>
> and I want to compare matrix b to matrix a to identify in which indices of matrix a is
>equal to the content of matrix b.
>
> For example for the number 230 in matrix b is located in the indices of #7 in matrix a
> or number 650 in matrix b is located in the indices of #8 in matrix a
>
> Thanks before for the answer
>
> Regards
>

one way:

-------------------------
a=[250;500;750;200;600;800;230;650;800];
b=sort(a,1);
N=length(a);
for i=1:N
     [~,N(i)]=ismember(b(i),a);
end
N
--------------

      4 7 1 2 5 8 3 9 9


--Nasser

Subject: Compare two vectors and got indices of the identical numbers

From: Nasser M. Abbasi

Date: 17 Feb, 2013 01:48:08

Message: 3 of 5

On 2/16/2013 6:52 PM, Nasser M. Abbasi wrote:
> On 2/16/2013 5:53 PM, Danny wrote:
>> How to compare 2 vectors and got the indices of 2 identical numbers
>>
>> lets say I have matrix a
>>>> a=[250;500;750;200;600;800;230;650;800]
>>
>> a =
>>
>> 250
>> 500
>> 750
>> 200
>> 600
>> 800
>> 230
>> 650
>> 800
>>
>> and I sort matrix a to matrix b
>>
>>>> b=sort(a)
>>
>> b =
>>
>> 200
>> 230
>> 250
>> 500
>> 600
>> 650
>> 750
>> 800
>> 800
>>
>> and I want to compare matrix b to matrix a to identify in which indices of matrix a is
>> equal to the content of matrix b.
>>
>> For example for the number 230 in matrix b is located in the indices of #7 in matrix a
>> or number 650 in matrix b is located in the indices of #8 in matrix a
>>
>> Thanks before for the answer
>>
>> Regards
>>
>
> one way:
>
> -------------------------
> a=[250;500;750;200;600;800;230;650;800];
> b=sort(a,1);
> N=length(a);
> for i=1:N
> [~,N(i)]=ismember(b(i),a);
> end
> N
> --------------
>
> 4 7 1 2 5 8 3 9 9
>

I just noticed this can be done in one command

K>> [~,J]=ismember(b,a)

J =

      4
      7
      1
      2
      5
      8
      3
      9
      9

Subject: Compare two vectors and got indices of the identical numbers

From: Nasser M. Abbasi

Date: 17 Feb, 2013 02:02:13

Message: 4 of 5

On 2/16/2013 7:48 PM, Nasser M. Abbasi wrote:
> On 2/16/2013 6:52 PM, Nasser M. Abbasi wrote:
>> On 2/16/2013 5:53 PM, Danny wrote:
>>> How to compare 2 vectors and got the indices of 2 identical numbers
>>>
>>> lets say I have matrix a
>>>>> a=[250;500;750;200;600;800;230;650;800]
>>>
>>> a =
>>>
>>> 250
>>> 500
>>> 750
>>> 200
>>> 600
>>> 800
>>> 230
>>> 650
>>> 800
>>>
>>> and I sort matrix a to matrix b
>>>
>>>>> b=sort(a)
>>>
>>> b =
>>>
>>> 200
>>> 230
>>> 250
>>> 500
>>> 600
>>> 650
>>> 750
>>> 800
>>> 800
>>>
>>> and I want to compare matrix b to matrix a to identify in which indices of matrix a is
>>> equal to the content of matrix b.
>>>
>>> For example for the number 230 in matrix b is located in the indices of #7 in matrix a
>>> or number 650 in matrix b is located in the indices of #8 in matrix a
>>>
>>> Thanks before for the answer
>>>
>>> Regards
>>>
>>
>> one way:
>>
>> -------------------------
>> a=[250;500;750;200;600;800;230;650;800];
>> b=sort(a,1);
>> N=length(a);
>> for i=1:N
>> [~,N(i)]=ismember(b(i),a);
>> end
>> N
>> --------------
>>
>> 4 7 1 2 5 8 3 9 9
>>

I was sleepy above. I meant to create a zero length array of length N
but ended up using N itself.

Ok, lets start over.

Now we'll find ALL locations in 'a' where 'b' value exist. Using
ismember only gives the largest index in 'a'. If that is ok
for you, then use ismember as in

[~,J]=ismember(b,a)

But to find all of them, then use find() instead of ismember.
But unless you want to use a cell, then you do not know the
size to preallocate for this. So using worst case, allocate
N by N matrix for the result (or use cell matrix).

----------------------
a=[250;500;750;200;600;800;230;650;800];
b=sort(a);
N=length(a);
M=zeros(N);
for i=1:N
    z=find(b(i)==a);
    M(i,1:length(z))=z;
end
M
-----------------------

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


For example, this ways the 8'th element in 'b' is found
in index 6 AND index 9 in 'a'.

If you like cell arrays, then use

M=arrayfun(@(i) find(b(i)==a),1:length(a),'UniformOutput',false)

--Nasser

Subject: Compare two vectors and got indices of the identical numbers

From: dpb

Date: 17 Feb, 2013 02:14:31

Message: 5 of 5

On 2/16/2013 5:53 PM, Danny wrote:
> How to compare 2 vectors and got the indices of 2 identical numbers
>
> lets say I have matrix a
>>> a=[250;500;750;200;600;800;230;650;800]
>
> a =
>
> 250
> 500
> 750
> 200
> 600
> 800
> 230
> 650
> 800
>
> and I sort matrix a to matrix b
>
>>> b=sort(a);
>
...

> and I want to compare matrix b to matrix a to identify in which indices
> of matrix a is equal to the content of matrix b.
> For example for the number 230 in matrix b is located in the indices of
> #7 in matrix a
> or number 650 in matrix b is located in the indices of #8 in matrix a
>

If b is always a sorted, then

[b,ix]=sort(a);

b will give you the value; ix the index where it was in a.

doc sort

--

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