我有一个 24 x 3 matrix
“point1”
我还有另外 5 个 1x3 row vectors
.我想要的是将所有 5 个不同的行向量与 “point1”
的每一行进行比较以便查看这 5 个向量中是否有任何一个在 “point1”
中有相应的行等于它们,然后返回 “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
返回:
pIndx = [2 6];
谢谢大家!
最佳答案
您可以使用 ismember
with the 'rows'
flag搜索向量和数据矩阵之间的交集。
使用上面的例子可能最容易concatenate您的查询向量到一个矩阵中并将其用作输入:
test = find(ismember(point1, vertcat(fN, fS, fE, bN, bT), 'rows'))
哪个返回:
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'));
关于matlab - 如何将多个行向量与矩阵进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44725933/