Get all row indices where values of to columns reach their maximum

1 回表示 (過去 30 日間)
Georg Bauer
Georg Bauer 2023 年 2 月 3 日
コメント済み: Les Beckham 2023 年 2 月 5 日
Hey,
i would like to get an array containing the indices of all rows in which the values of column 2 and 3 reach their maximum at the same time.
Example:
A = [1 2 3;
1 4 3;
2 4 3;
1 4 2];
Output should be an array containing the values 2 and 3 corresponding to row 2 and 3 of array A since the values in column 2 and 3 both reach their maximum at the same time.
Output = [2 3];
Thank You :-)

採用された回答

Les Beckham
Les Beckham 2023 年 2 月 3 日
A = [1 2 3;
1 4 3;
2 4 3;
1 4 2];
m = max(A(:,2:end), [], 1)
m = 1×2
4 3
Output = find(all(ismember(A(:,2:end), m), 2))
Output = 2×1
2 3
  4 件のコメント
Voss
Voss 2023 年 2 月 4 日
@Georg Bauer: What should Output be when A is as follows?
A = [1 2 3;
1 4 3;
2 3 3;
1 4 2];
% get column-wise maxima:
m = max(A(:,2:end), [], 1);
The given answer will give [2; 3] since rows 2 and 3 both have all values in their 2nd and 3rd columns as elements of the column-wise maxima m ([3 4]):
Output = find(all(ismember(A(:,2:end), m), 2))
Output = 2×1
2 3
But I think you want the result to be row 2 only, since that's the only row where the column-wise maxima occur in the same row, in which case you can use this logic, to compare elements in each column individually:
Output = find(all(A(:,2:end) == repmat(m,size(A,1),1), 2))
Output = 2
Les Beckham
Les Beckham 2023 年 2 月 5 日
Good catch, Voss. Thanks for pointing that out.

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

その他の回答 (0 件)

カテゴリ

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