Extract the Maximum Column elements of every Row

1 回表示 (過去 30 日間)
Pankaja Tanjore
Pankaja Tanjore 2015 年 12 月 19 日
回答済み: Guillaume 2015 年 12 月 19 日
Hello , I have an array
A = [8 4; 3 6; 2 7; 1 4; 2 3;2 1;3 1; 3 5; 8 6; 8 1];
I want to extract maximum column elements for every Row.
In the above array A i have
3 1 ; 3 5 ; 3 6
I want only 3 6 to be retained as that is the maximum column for that row.
Similarly i have 8 1 ;8 4; 8 6 I want only 8 6 as that has the maximum column value.
Final output I want for the array A is as follows
ans =
1 4;
2 7;
3 6;
8 6;
Looking forward to hear from you at the earliest
Thanks
Pankaja

採用された回答

Guillaume
Guillaume 2015 年 12 月 19 日
Your wording is rather poor. It looks like what you want to do is for each unique value in the first column find the corresponding maximum in the second column. This is trivial to obtain with unique and accumarray:
A = [8 4; 3 6; 2 7; 1 4; 2 3;2 1;3 1; 3 5; 8 6; 8 1];
[val, ~, subs] = unique(A(:, 1));
B = [val, accumarray(subs, A(:, 2), [], @max)]

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by