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:
Finding if a row is in a matrix or not

Subject: Finding if a row is in a matrix or not

From: Diego Zegarra

Date: 21 Feb, 2009 17:24:01

Message: 1 of 5

Assume we have a matrix A,

A=[3 2 1 10;
     5 1 2 8;
     7 4 1 9;
     6 9 2 3];

I want to see if the first 3 elements of any row are equal to a vector I have, so lets say I have a vector [5 1 2] and I want to see if it is or not in the matrix A. If it is then I can get a 1 for true, or a 0 for false. In case I have a vector [1 2 8] it should give a 0 as it is not the same as any first three elements of any row.

I know strfind does the work when we have one column or row vector, but I do not know when we have a matrix. I know I can do a loop through that but I am checking if there is a straight way and faster way to do it.

for i = 1:size(A,1)
    ToF = strfind(A(i,:),[5 1 2]);
    if ToF == 1
        break
    end
end

Hope I made myself clear, if not let me know.

Thanks!

Subject: Finding if a row is in a matrix or not

From: Jos

Date: 21 Feb, 2009 17:56:01

Message: 2 of 5

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gnpdbh$3sh$1@fred.mathworks.com>...
> Assume we have a matrix A,
>
> A=[3 2 1 10;
> 5 1 2 8;
> 7 4 1 9;
> 6 9 2 3];
>
> I want to see if the first 3 elements of any row are equal to a vector I have, so lets say I have a vector [5 1 2] and I want to see if it is or not in the matrix A. If it is then I can get a 1 for true, or a 0 for false. In case I have a vector [1 2 8] it should give a 0 as it is not the same as any first three elements of any row.
>
> I know strfind does the work when we have one column or row vector, but I do not know when we have a matrix. I know I can do a loop through that but I am checking if there is a straight way and faster way to do it.
>
> for i = 1:size(A,1)
> ToF = strfind(A(i,:),[5 1 2]);
> if ToF == 1
> break
> end
> end
>
> Hope I made myself clear, if not let me know.
>
> Thanks!

Use ISMEMBER on the first three columns of A. See the help how to use it on rows.

hth
Jos

Subject: Finding if a row is in a matrix or not

From: Diego Zegarra

Date: 21 Feb, 2009 18:25:03

Message: 3 of 5

Thanks Jos, it would work like this,

tf = sum(ismember(A(:,1:3), [5 1 2], 'rows'))

Subject: Finding if a row is in a matrix or not

From: Marcello Ferro

Date: 29 Mar, 2011 15:53:03

Message: 4 of 5

"Diego Zegarra" wrote in message <gnpgtv$1hh$1@fred.mathworks.com>...
> Thanks Jos, it would work like this,
>
> tf = sum(ismember(A(:,1:3), [5 1 2], 'rows'))

For this purpose I posted a mfind matlab function that finds the matching rows or columns in a matrix

   [ I B ] = mfind(M, X)

   Find the row or column vector X within the matrix M.
   If X is a row vector, I will contain the column indexes where X is a row in M.
   If X is a column vector, I will contain the row indexes where X is a column in M.
   B will contain the boolean matching rows or columns.
   If no match if found, I will be empty.

   Example:
      a = [1 2; 3 4; 5 6];

      [i b] = mfind(a, [3 4])
      i = 2
      b = [ 0; 1; 0 ]

      [i b] = mfind(a, [1; 3; 5])
      i = 1
      b = [ 1 0 ]

The MFIND script may be downloaded here in MATALB central files exchange

Subject: Finding if a row is in a matrix or not

From: Niyas Kuzhivelipurath

Date: 12 Oct, 2012 04:37:09

Message: 5 of 5

A =

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

a = [ 5 1 2]

To find whether the vector a is occurring in the first three columns of the original matrix A we can give ,

find(ismember(A(:,1:3),a,'rows'),1)

ans =

      2

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