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

1 回表示 (過去 30 日間)
Yaser Khojah
Yaser Khojah 2020 年 3 月 17 日
コメント済み: Yaser Khojah 2020 年 3 月 17 日
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 日
  3 件のコメント
madhan ravi
madhan ravi 2020 年 3 月 17 日
Illustrate the answer if the B were to be
B = [2, 6, 4; 2, 5 , 9];
Yaser Khojah
Yaser Khojah 2020 年 3 月 17 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by