作者:不要哭开心就好_723 | 来源:互联网 | 2023-01-31 13:46
我有一个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];
多谢你们!
1> excaza..:
你可以用ismember
与'rows'
标志来搜索您的载体和你的数据矩阵之间的交叉点.
使用上面的示例,将查询向量连接到一个矩阵并将其用作输入可能是最简单的:
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'));