A compact way to find max in one column with a condition on the second column

1 回表示 (過去 30 日間)
Sim
Sim 2022 年 10 月 7 日
コメント済み: Sim 2022 年 10 月 7 日
Given
a = [35 -1
21 1
11 2];
I want the max in the first column, but the max cannot be that one where there is a "-1" in the second column.
Therefore the correct answer is not "35" (since in the second column there is "-1"), but "21".
Do you know a compact way to get this max in the first column, with the condition on the second column ?

採用された回答

Bruno Luong
Bruno Luong 2022 年 10 月 7 日
a = [35 -1
21 1
11 2];
max(a(a(:,2) ~= -1,1))
ans = 21
  3 件のコメント
Bruno Luong
Bruno Luong 2022 年 10 月 7 日
If you want to get row index of the max value:
a = [35 -1
21 1
11 2];
filteridx = find(a(:,2) ~= -1);
[maxvalue, maxidx] = max(a(filteridx,1));
maxidx = filteridx(maxidx)
maxidx = 2
Sim
Sim 2022 年 10 月 7 日
thanks a lot! :-)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by