compare the column 2 of two matrix and forming the matrix with same entries in column 2 of Second Matrix.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Jan
2022-3-19
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!