Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Compare two vectors and got indices of the identical numbers
Replies: 4   Last Post: Feb 16, 2013 9:14 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Nasser Abbasi

Posts: 5,700
Registered: 2/7/05
Re: Compare two vectors and got indices of the identical numbers
Posted: Feb 16, 2013 8:48 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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






Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.