compare the column 2 of two matrix and forming the matrix with same entries in column 2 of Second Matrix.
1 view (last 30 days)
Show older comments
I have First Matrix A1 with 8 rows and 3 column, The 2 column of Matrix A1 and Matrix A2 have some same values. I want another matrix A3 which will have common Column 2 values and column 3 has same entries as in column 3 of matrix A1.
A1 =
[115.28 30 1
115.26 33 2
115.25 8 3
115.24 21 2
115.24 25 1
115.21 2 2
115.21 18 2
115.19 1 3]
2nd matrix
A2 =
[115.19 1 3
115.21 2 3
115.25 8 3
115.24 25 3
115.28 30 3
115.26 33 3]
I want output A3 as:
A3 =
[115.19 1 3
115.21 2 2
115.25 8 3
115.24 25 1
115.28 30 1
115.26 33 2]
0 Comments
Accepted Answer
Jan
on 19 Mar 2022
A1 = [115.28 30 1; ...
115.26 33 2; ...
115.25 8 3; ...
115.24 21 2; ...
115.24 25 1; ...
115.21 2 2; ...
115.21 18 2; ...
115.19 1 3];
A2 = [115.19 1 3; ...
115.21 2 3; ...
115.25 8 3; ...
115.24 25 3; ...
115.28 30 3; ...
115.26 33 3];
[lA2, iA1] = ismember(A2(:, 1), A1(:, 1), 'legacy');
A3 = [A2(lA2, 1:2), A1(iA1, 3)]
The 'legacy' flag is required, because A1 contains the numer 115.24 twice. In legacy mode, the last occurrence is chosen, in normal mode (in modern Matlab versions), it is the first one.
0 Comments
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!