How to compare and combine two matrix

18 ビュー (過去 30 日間)
Ville Koivunen
Ville Koivunen 2019 年 3 月 20 日
コメント済み: Ville Koivunen 2019 年 3 月 21 日
Hello
I'm new into matlab and could'nt find help to my problem.
I have two large dataset that I want to combine so that each line corresponds to right value. In my dataset every line has a unique id (matrix column 1) and i want to find the lines with same id in a two different dataset and combine them into one.
So:
How can I do a loop that compares a value in two different matrix and then writes the result as a new. The value that is compared is written only once in outcome.
Lets say I have matrix A1 and A2 and I want a loop that compares if the values in first row are the same in A1 and A2. If this happen it combines the lines to a new variable. So in this case I would compare the first value in first column --> in line 2 and line 4 the first number match--> Result is the lines added without the first column in variable 2.
A1 =
3 3 5 3
8 1 5 2
6 4 5 6
7 4 7 1
A2 =
6 3 5
8 4 3
9 7 9
7 5 1
Outcome A3=
8 1 5 2 4 3
7 4 7 1 5 1
I have a code already that compares the value but how to generate the matrix A3 in a loop.
[m,n] = size(A1);
for i = 1:n
if isequal (A1(1,i),A2(1,i));
B(i,:)=[A1(i,:),A2(i,:)];
end
end
B
Cheers

採用された回答

madhan ravi
madhan ravi 2019 年 3 月 20 日
idx=A1(:,1)==A2(:,1);
A3=[A1(idx,:),A2(idx,2:end)]
  9 件のコメント
madhan ravi
madhan ravi 2019 年 3 月 21 日
編集済み: madhan ravi 2019 年 3 月 21 日
[ ii, lo ] = ismember( A1(:,1), A2(:,1) );
A3 = [ A1, A2( lo(ii), 2:end ) ]
Ville Koivunen
Ville Koivunen 2019 年 3 月 21 日
Oh yes, now it works Thank you sir

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by