简体   繁体   English

如何在 matlab 中的矩阵中找到等于向量的行? 在,特别是我想要索引

[英]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].我有一个向量 A=[1 2 3] 和我有一个矩阵 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我试过 find 但没有用

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.一种方法是使用 for 循环迭代地提取矩阵B每一行,并使用 if 语句将每一行与向量A进行比较。

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.您可以将 ismember 函数用于该问题的单行解决方案。

The usage will be like this:用法如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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