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:
Is there any non loop implement to find the index of a vector in the matrix?

Subject: Is there any non loop implement to find the index of a vector in the matrix?

From: zedong

Date: 14 Dec, 2008 13:29:02

Message: 1 of 7

Is there any non loop implement to find the index of a vector in the matrix?
for example:
a=[1 2]
b=[4 5;6 7;8 7;1 2;2 3]
I want to return 4
(which is the index of a in b)
Thank you !

Subject: Is there any non loop implement to find the index of a vector in the matrix?

From: zedong

Date: 14 Dec, 2008 13:39:02

Message: 2 of 7

example 2:
a=[1 2;5 4]
 b=[4 5;6 7;8 7;1 2;2 3]
I want to return [4;1]
(I think [4 5] is the same as [5 4])

Subject: Is there any non loop implement to find the index of a vector in the matrix?

From: Florian

Date: 14 Dec, 2008 13:49:02

Message: 3 of 7

How about this:

a=[1 2];
b=[4 5;6 7;8 7;1 2;2 3];
%return the row index where theres the number you are searching for
firstcolumnrow=find(b(:,1)==a(1,1))
secondcolumnrow=find(b(:,2)==a(1,2))

if firstcolumnrow and seconcolumnrow are equal this is the row that you are searching for

"zedong
" <zdongwu@gmail.com> wrote in message <gi31mu$sqn$1@fred.mathworks.com>...
> Is there any non loop implement to find the index of a vector in the matrix?
> for example:
> a=[1 2]
> b=[4 5;6 7;8 7;1 2;2 3]
> I want to return 4
> (which is the index of a in b)
> Thank you !

Subject: Is there any non loop implement to find the index of a vector in the matrix?

From: hailiang shen

Date: 14 Dec, 2008 15:05:04

Message: 4 of 7

you can change the matrix to a 'char', and use 'findstr' function.

For example,
a=num2str([1, 2]);
b=num2str(reshape([4 5;6 7;8 7;1 2;2 3]', 1, 10);
findstr(b,a)

"zedong
" <zdongwu@gmail.com> wrote in message <gi31mu$sqn$1@fred.mathworks.com>...
> Is there any non loop implement to find the index of a vector in the matrix?
> for example:
> a=[1 2]
> b=[4 5;6 7;8 7;1 2;2 3]
> I want to return 4
> (which is the index of a in b)
> Thank you !

Subject: Is there any non loop implement to find the index of a vector in the matrix?

From: us

Date: 14 Dec, 2008 15:21:02

Message: 5 of 7

"zedong"
> Is there any non loop implement to find the index of a vector in the matrix?
> for example:
> a=[1 2]
> b=[4 5;6 7;8 7;1 2;2 3]
> I want to return 4

one of the solutions

% the data
     a=[
          1,2 % <- pos 4
          4,5 % <- pos 1
     ];
     b=[4 5;6 7;8 7;1 2;2 3];
% the engine
     [ix,ix]=ismember(a,b,'rows');
% the result
     ix
%{
     ix =
          4
          1
%}

us

Subject: Is there any non loop implement to find the index of a vector in the matrix?

From: zedong

Date: 14 Dec, 2008 15:53:02

Message: 6 of 7

"us " <us@neurol.unizh.ch> wrote in message <gi388u$ed0$1@fred.mathworks.com>...
> "zedong"
> > Is there any non loop implement to find the index of a vector in the matrix?
> > for example:
> > a=[1 2]
> > b=[4 5;6 7;8 7;1 2;2 3]
> > I want to return 4
>
> one of the solutions
>
> % the data
> a=[
> 1,2 % <- pos 4
> 4,5 % <- pos 1
> ];
> b=[4 5;6 7;8 7;1 2;2 3];
> % the engine
> [ix,ix]=ismember(a,b,'rows');
> % the result
> ix
> %{
> ix =
> 4
> 1
> %}
>
> us


Thank you very much.you have done a wonderful work.Thank you

Subject: Is there any non loop implement to find the index of a vector in the matrix?

From: Roger Stafford

Date: 14 Dec, 2008 18:04:02

Message: 7 of 7

"zedong
" <zdongwu@gmail.com> wrote in message <gi3a4u$r0s$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gi388u$ed0$1@fred.mathworks.com>...
> .....
> > [ix,ix]=ismember(a,b,'rows');
> ......

  I believe you will need to sort the rows of a and b before using Urs's 'ismember' technique, if you want such rows as [4,5] and [5,4] to be regarded as identical:

 [ix,ix]=ismember(sort(a,2),sort(b,2),'rows');

Roger Stafford

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