2017-06-23 4 views
2

私は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]; 

みんなありがとうを!

答えて

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')); 
+0

ありがとう@excaza。私は特にライナーが好きです。私は本当に感謝しています... – User1772

0

次の操作を試みることができる:

[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))] 
+0

ありがとうございました! sも同様 – User1772

関連する問題