Compare row by row of two arrays and write in array, if there are matching values

13 ビュー (過去 30 日間)
Hey... how to find matching values between 3 arrays?
A=[1 2 0 3; 3 0 4 0; 6 7 0 0]; B=[1 8 0; 2 3 0 ; 6 1 8]; C=(1 3; 0 0; 5 6 7);
I want to find the values of matching values between those 3 arrays.
Result should be:
result=[1; 0; 6]
There will always be just one match out of these arrays. Therefore I will receive a 1x3 matrix.
0 should not be recognized as a match.
As you can see, the width of the rows is variable. The length of the array is the same for A, B and C.
I tried it with find in a for-loop, but got the matching of the whole arrays and not rows-specific.
Thanks,
Joshi

採用された回答

Bob Thompson
Bob Thompson 2019 年 8 月 19 日
編集済み: Bob Thompson 2019 年 8 月 19 日
I think intersect is a much better choice here than find.
Because you are looking for specific row results I'm not sure how to do this without a for loop, but this is my idea.
A = [1 2 0 3; 3 0 4 0; 6 7 0 0];
B = [1 8 0; 2 3 0 ; 6 1 8];
C = [1 3; 0 0; 5 6];
result = zeros(size(A,1),1);
for i = 1:size(A,1)
tmp = intersect(A(i,:),B(i,:));
tmp = intersect(tmp,C(i,:));
if length(tmp)>1
tmp = tmp(tmp~=0);
end
result(i) = tmp;
end
  2 件のコメント
Joshua Lindner
Joshua Lindner 2019 年 8 月 19 日
Thanks for you reply!
But this just works for arrays of the same size. In my case I get an error...
Bob Thompson
Bob Thompson 2019 年 8 月 19 日
Sorry, I had a typo.
if length(tmp)>1
Replace the line.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by