Extract the Maximum Column elements of every Row

1 view (last 30 days)
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

Accepted Answer

Guillaume
Guillaume on 19 Dec 2015
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)]

More Answers (0)

Community Treasure Hunt

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

Start Hunting!