在矩阵中找到匹配的行

问题描述:

假设我有一个(m x n)矩阵Q和一个行向量r,例如

Suppose I have an (m x n) matrix Q, and a row vector r, e.g.

Q = [ 1 2 3 ; 4 2 3 ; 5 6 7 ; 1 2 3 ; 1 2 3 ; 1 2 5 ];

r = [ 1 2 3 ];

获得表示长度为m的逻辑向量的最简单方法是什么,该逻辑向量指示Q中的哪行(对于所有元素)与指定的行r相同?

What is the easiest way to obtain a logical vector (of length m) that indicates which of the rows in Q are identical (for all elements) to the specified row r?

在上面的示例中,应该是

In the sample case above, that should be

[ 1 0 0 1 1 0 ];

您可以使用 ismember 并在一行中完成:

You can use ismember and do it in a single line:

>> ismember(Q,r,'rows')'

ans =

     1     0     0     1     1     0