Thread Subject: Find a vector in a matrix

Subject: Find a vector in a matrix

From: Jonathan

Date: 13 Aug, 2008 17:41:02

Message: 1 of 5

Is there an easy way to find a vector in a matrix?

for example

     1 1 1 1 1 1
     1 1 1 1 1 1
    12 12 12 12 12 12
     1 1 1 1 1 1
     1 1 1 1 1 1

i want to identify the row that has [12 12 12 12 12 12]

Thanks

Subject: Find a vector in a matrix

From: Praetorian

Date: 13 Aug, 2008 18:08:11

Message: 2 of 5

On Aug 13, 11:41=A0am, "Jonathan " <n...@domain.com> wrote:
> Is there an easy way to find a vector in a matrix?
>
> for example
>
> =A0 =A0 =A01 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1
> =A0 =A0 =A01 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1
> =A0 =A0 12 =A0 =A012 =A0 =A012 =A0 =A012 =A0 =A012 =A0 =A012
> =A0 =A0 =A01 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1
> =A0 =A0 =A01 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1 =A0 =A0 1
>
> i want to identify the row that has [12 12 12 12 12 12]
>
> Thanks

data =3D ones(5, 6);
data(3,:) =3D 12
find(all(data'=3D=3D12))

data =3D

     1 1 1 1 1 1
     1 1 1 1 1 1
    12 12 12 12 12 12
     1 1 1 1 1 1
     1 1 1 1 1 1


ans =3D

     3

Subject: Find a vector in a matrix

From: Dan Hensley

Date: 13 Aug, 2008 19:12:07

Message: 3 of 5

Jonathan wrote:
> Is there an easy way to find a vector in a matrix?
>
> for example
>
> 1 1 1 1 1 1
> 1 1 1 1 1 1
> 12 12 12 12 12 12
> 1 1 1 1 1 1
> 1 1 1 1 1 1
>
> i want to identify the row that has [12 12 12 12 12 12]
>
> Thanks

There's a strfind trick to do this. It's very fast.

Assuming a is your matrix, and b is your vector,

 >> ind=strfind(reshape(a',1,[]),b)

Now if you're looking for a specific row, you need to make sure that the
index corresponds to the start of a row:

ind = ind((ind-1)/size(a,2) == fix((ind-1)/size(a,2)))


Dan

Subject: Find a vector in a matrix

From: Ilya Rozenfeld

Date: 13 Aug, 2008 20:02:02

Message: 4 of 5

It looks like using "strmatch" would be even easier:

data =ones(5, 6);
data(3,:) =12;

x = ones(1,6)*12;
strmatch(x, data)


Dan Hensley <somewhere@over.there> wrote in message <q8-
dnUAG6IN0rD7VnZ2dnUVZ_rXinZ2d@forethought.net>...
> Jonathan wrote:
> > Is there an easy way to find a vector in a matrix?
> >
> > for example
> >
> > 1 1 1 1 1 1
> > 1 1 1 1 1 1
> > 12 12 12 12 12 12
> > 1 1 1 1 1 1
> > 1 1 1 1 1 1
> >
> > i want to identify the row that has [12 12 12 12 12 12]
> >
> > Thanks
>
> There's a strfind trick to do this. It's very fast.
>
> Assuming a is your matrix, and b is your vector,
>
> >> ind=strfind(reshape(a',1,[]),b)
>
> Now if you're looking for a specific row, you need to
make sure that the
> index corresponds to the start of a row:
>
> ind = ind((ind-1)/size(a,2) == fix((ind-1)/size(a,2)))
>
>
> Dan

Subject: Find a vector in a matrix

From: Dan Hensley

Date: 13 Aug, 2008 21:24:19

Message: 5 of 5

Ilya Rozenfeld wrote:
> It looks like using "strmatch" would be even easier:
>
> data =ones(5, 6);
> data(3,:) =12;
>
> x = ones(1,6)*12;
> strmatch(x, data)


It's a matter of performance. strfind is by far the fastest of the methods.

Dan






>
>
> Dan Hensley <somewhere@over.there> wrote in message <q8-
> dnUAG6IN0rD7VnZ2dnUVZ_rXinZ2d@forethought.net>...
>> Jonathan wrote:
>>> Is there an easy way to find a vector in a matrix?
>>>
>>> for example
>>>
>>> 1 1 1 1 1 1
>>> 1 1 1 1 1 1
>>> 12 12 12 12 12 12
>>> 1 1 1 1 1 1
>>> 1 1 1 1 1 1
>>>
>>> i want to identify the row that has [12 12 12 12 12 12]
>>>
>>> Thanks
>> There's a strfind trick to do this. It's very fast.
>>
>> Assuming a is your matrix, and b is your vector,
>>
>> >> ind=strfind(reshape(a',1,[]),b)
>>
>> Now if you're looking for a specific row, you need to
> make sure that the
>> index corresponds to the start of a row:
>>
>> ind = ind((ind-1)/size(a,2) == fix((ind-1)/size(a,2)))
>>
>>
>> Dan
>

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com