简体   繁体   中英

How can I find a row equal to a vector in a matrix in matlab ? In, particular I would like to have the indexes

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

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM