How to compare and merge matrices with same numbers?
Show older comments
I have a question about a code. I have two matrices, A and B. I would like to compare these two matrices based on the values of the columns 2 and 3. for example:
A=[1 2 3 4
9 6 7 8
10 5 4 7]
and
B= [8 2 3 4
11 6 7 8
5 5 6 7]
It is noticed that rows have same values of 2nd and 3rd column.
After the comparison of these two matrices I would like to create a new matrix C which would include keep the same rows of A and Bmatrix and
and finally to merge them (I want to keep the rows of table A that have the same values as columns 2 and 3 of table B. Finally, I want to build a matrix which will have the rows of table A and the rows of B that have the same value in the 2nd and 3rd columns)
I mean C= [ 1 2 3 4
9 6 7 8
10 5 4 7
5 5 6 7 ]
I have tried
clc
clear
T1=readmatrix('file1.txt');
T2=readmatrix('file2.txt');
A=T1(:,2:3);
B=T2(:,2:3);
C=[A;B];
C(:,end) = [];
[~,~,jj] = unique(C,'rows','stable');
C([false; diff(jj) == 0],:) = [];
writematrix(C,'output.txt','delimiter','\t');
could you please help me?
Answers (1)
A =[ 1 2 3 4; ...
9 6 7 8; ...
10 5 4 7];
B = [ 8 2 3 4; ...
11 6 7 8; ...
5 5 6 7];
m = ismember(B(:, 2:3), A(:, 2:3), 'rows');
C = cat(1, A, B(~m, :))
8 Comments
Ivan Mich
on 16 Dec 2022
Jan
on 16 Dec 2022
@Ivan Mich: The elements of arrays must have the same type in Matlab. Only cell arrays can contain elements of different types and sizes. This means, that you cannot create this A and B an in consequence cannot create C.
So reformulate the question using some valid input arguments. Then the needed code should be exactly, what I have posted already.
Peter Perkins
on 19 Dec 2022
"Only cell arrays can contain elements of different types and sizes." Jan, for different types at least, that's not been true for many years.
>> t1 = table([1;9;10],[2;6;5],[3;7;4],[4;8;7],["USA";"LA";"TEX"])
t1 =
3×5 table
Var1 Var2 Var3 Var4 Var5
____ ____ ____ ____ _____
1 2 3 4 "USA"
9 6 7 8 "LA"
10 5 4 7 "TEX"
>> t2 = table([8;11;5],[2;6;5],[3;7;6],[4;7;7],["USA";"LA";"MA"])
t2 =
3×5 table
Var1 Var2 Var3 Var4 Var5
____ ____ ____ ____ _____
8 2 3 4 "USA"
11 6 7 7 "LA"
5 5 6 7 "MA"
>> tf = ismember(t1(:,2:3),t2(:,2:3),"rows")
tf =
3×1 logical array
1
1
0
>> [t1; t2(~tf,:)]
ans =
4×5 table
Var1 Var2 Var3 Var4 Var5
____ ____ ____ ____ _____
1 2 3 4 "USA"
9 6 7 8 "LA"
10 5 4 7 "TEX"
5 5 6 7 "MA"
Jan
on 19 Dec 2022
@Peter Perkins: Okay, I did not consider a table as an elementary array. They are more similar to a struct, such that the columns are equivalent to the fields and can have different types.
Peter Perkins
on 19 Dec 2022
Fair enough. I will say, though, that while "like a scalar struct containing vectors" is one way to think about tables, it's equally valid to think about them as "like a double, or string, or [your favorite type of data] array with much of the same array functionality built around a rectangular topology, but allowing multiple types".
Tables for thie use in this post are certainly better than cell arrays.
Jan
on 19 Dec 2022
@Peter Perkins: My point was, that:
A =[ 1 2 3 4 USA; ...
9 6 7 8 LA; ...
10 5 4 7 TEX];
is not a valid Matlab matrix, especially if it was an expanded:
C= [ 1 2 3 4; ...
9 6 7 8; ...
10 5 4 7; ...
5 5 6 7];
If the OP specifies if tables or cell arrays are meant, the answer can be adjusted.
Peter Perkins
on 4 Jan 2023
Sure. You know that the first height(t1) rows in the result came from t1, and the last sum(~tf) rows came from t2. Add a variable to the result that's something like [ones(height(t1),1); 2*ones(sum(~tf),1)].
Categories
Find more on Logical 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!