Home > other >  How can I find a row equal to a vector in a matrix in matlab ? In, particular I would like to have t
How can I find a row equal to a vector in a matrix in matlab ? In, particular I would like to have t

Time:10-31

I have a vector A=[1 2 3] and I have a matrix B=[5 6 8,1 2 3, 9 6 5]. How can I find the row indexes? I tried find but it didn't work

CodePudding user response:

One way is to use a for-loop to extract each row of matrix B iteratively and compare each row to vector A by using an if-statement.

A = [1 2 3];

B = [5 6 8; 1 2 3; 9 6 5];

[Number_Of_Rows,Number_Of_Columns] = size(B);

for Row = 1: Number_Of_Rows
   if B(Row,:) == A
      fprintf("Matching row is "   Row);
   end
end

CodePudding user response:

You can use the ismember function for a one-line solution to that question.

The usage will be like this:

[tf, index]=ismember(A,B,'rows');
  • Related