How to compare and combine two matrix

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

 Accepted Answer

idx=A1(:,1)==A2(:,1);
A3=[A1(idx,:),A2(idx,2:end)]

9 Comments

2016a <= versions
idx=bsxfun(@eq,A1(:,1),A2(:,1));
A3=[A1(idx,:),A2(idx,2:end)]
Well that was easy, thanks. Apparently thi sworks also if I have more than two matrix to compare
Hello
Apparently this doesent work when the sizes are not equal. My first matrix has hundreds of rows and other one just few. Should i resize the matrix or is there another way.
madhan ravi
madhan ravi on 21 Mar 2019
Edited: madhan ravi on 21 Mar 2019
Simply illustrate with an example and explicitly state your desired result with 2 by 3 and 3 by 3 matrix or whatever.
A2=[1 2 3; 4 5 6; 7 8 9];
A1=[4 3; 1 2];
idx=A1(:,1)==A2(:,1);
A3=[A1(idx,:),A2(idx,2:end)];
this script gives an error
Matrix dimensions must agree.
Error in Untitled6 (line 30)
idx=A1(:,1)==A2(:,1);
As the number four is the common variable in column 1. Desired outcome would be in this case.
A3=
4 3 5 6
Why did you exclude one? Explain the logic behind it.
Well in my real data is in 75890x9 and 40x6 matrixes and I just wanted to sho example. Rows have a common factor on line 1 and I want to combine them. So I could handle the measured data in one piece and not to manually find the coresponding rows. Row 1 just indicates the measurement point.
Maybe I have been wrong and it would be better to handle data as tables. But still I need to do the same thing.
Example (little part) from real data
A1=[...
184363 1119 0 3
184370 1766 3 25];
A2=[...
184363 404 0.36 0.58 0.71 0.60 0.463
184364 128 0.47 0.63 0.76 0.63 0.57
184365 43 0.61 0.74 0.831 0.76 0.65
184370 13 0.55 0.61 0.70 0.60 0.565];
outcome desired
A3=[...
184363 1119 0 3 404 0.36 0.58 0.71 0.60 0.463
184370 1766 3 25 13 0.55 0.61 0.70 0.60 0.565];
[ ii, lo ] = ismember( A1(:,1), A2(:,1) );
A3 = [ A1, A2( lo(ii), 2:end ) ]
Oh yes, now it works Thank you sir

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!