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)
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]

Accepted Answer

Jan
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)]
A3 = 6×3
115.1900 1.0000 3.0000 115.2100 2.0000 2.0000 115.2500 8.0000 3.0000 115.2400 25.0000 1.0000 115.2800 30.0000 1.0000 115.2600 33.0000 2.0000
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.

More Answers (0)

Categories

Find more on Particle & Nuclear Physics 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!