Compare two matrices and select max one based on the a column

I have two matrixes and I want to compare the last column. Then select the max one and the whole corresponding row.
For example
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
Guillaume 2020년 3월 17일
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.

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 3월 17일
C = max(A,B)

댓글 수: 3

Yaser Khojah
Yaser Khojah 2020년 3월 17일
편집: Yaser Khojah 2020년 3월 17일
It does not work in my example. I have to select the max of the last column then select its row? This one would compare element by element
Illustrate the answer if the B were to be
B = [2, 6, 4; 2, 5 , 9];
Something like this without the forloop
C = zeros(size(A));
for i = 1:11
c = [A(i,:);B(i,:)];
[Max_v, idx] = max(c(:,end));
C(i,:) = c(idx,:);
end

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2020년 3월 17일

댓글:

2020년 3월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by