如何将多个行向量与矩阵进行比较?

How to compare more than one row vector to a matrix?

我有一个 24 x 3 matrix “point1” 我还有另外 5 个 1x3 row vectors。我想要的是将所有 5 个不同的行向量与 “point1” 的每一行进行比较,以查看这 5 个向量中的任何一个是否在 “point1” 中具有与它们相等的对应行,然后return “point1” 中该行的索引。我已经能够用下面的代码做到这一点,但我正在寻找一个更简单和优雅(可能没有循环?)的解决方案。

point1 = [7.5   4   5
          8.5   4   5
         9.5    4   5
         10.5   4   5
         11.5   4   5
         7      4   5.5
         12     4   5.5
         6.5    4   6
        12.5    4   6
           6    4   6.5
          13    4   6.5
         5.5    4   7
        13.5    4   7
           5    4   7.5
          14    4   7.5
           5    4   8.5
          14    4   8.5
           5    4   9.5
          14    4   9.5
           5    4   10.5
          14    4   10.5
           5    4   11.5
          14    4   11.5
         5.5    4   12];

fN = [8, 4.5, 5];
fS = [8, 3.5, 5];
fE = [8.5, 4, 5];     
bN = [7, 4.5, 5];  
bT = [7, 4, 5.5];

for ii = 1:size(point1, 1)         
 indx(ii) = isequal(point1(ii,:),fN(:)') | isequal(point1(ii,:),fS(:)') | isequal(point1(ii,:),fE(:)') | isequal(point1(ii,:),bN(:)') | isequal(point1(ii,:),bT(:)') 
 pIndx = find(indx)      
end

这个returns:

pIndx = [2 6];

谢谢大家!

您可以尝试以下方法:

[find(all(fN == point1, 2)), ...
find(all(fS == point1, 2)), ...
find(all(fE == point1, 2)), ...
find(all(bN == point1, 2)), ...
find(all(bT == point1, 2))]

您可以使用 ismember with the 'rows' flag 来搜索向量和数据矩阵之间的交集。

使用上面的示例,将您的查询向量 concatenate 放入一个矩阵并将其用作输入可能是最简单的方法:

test = find(ismember(point1, vertcat(fN, fS, fE, bN, bT), 'rows'))

哪个returns:

test =

     2
     6

或者,如果单个结果很重要,您可以单独进行查询:

test_fN = find(ismember(point1, fN, 'rows'));
test_fS = find(ismember(point1, fS, 'rows'));
test_fE = find(ismember(point1, fE, 'rows'));
test_bN = find(ismember(point1, bN, 'rows'));
test_bT = find(ismember(point1, bT, 'rows'));