Compare two matrices and select max one based on the a column
1 view (last 30 days)
Show older comments
Commented: Yaser Khojah on 17 Mar 2020
I have two matrixes and I want to compare the last column. Then select the max one and the whole corresponding row.
A = [ 1 , 4, 5; 1, 4, 6];
B = [2, 6, 6; 2, 5 , 9];
The next matrix based on the last column max will be
C = [2, 6, 6; 2, 5 , 9];
Anyway to help, please
Guillaume on 17 Mar 2020
If I understood correctly:
C = A;
replacebyB = B(:, end) > A(:, end);
C(replacebyB, :) = B(replacebyB, :);
The above gives priority to A when the last columns are equal.
More Answers (1)
madhan ravi on 17 Mar 2020
C = max(A,B)
Find more on Matrix Indexing 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!