MATLAB: Find Max for Every Category

max

I have a 30 x 2 matrix. Column 1 has integers between 1 and 10 with each integer appearing at least once. Column 2 has random numbers between 1 and 1000. I want a 10X2 matrix where column 2 shows the max value from all rows that has the same integer in column 1
e.g. A = [1 12; 1 16; 2 5; 2 13; 3 9; 3 19; 3 50]
I want B = [1 16;2 13; 3 50]
Help much appreciated.
Thanks

Best Answer

Try this:
A = [1 12; 1 16; 2 5; 2 13; 3 9; 3 19; 3 50];
Au = unique(A(:,1));
B = [Au accumarray(A(:,1), A(:,2), [], @max)]
B =
1 16
2 13
3 50
The unique call is necessary to create the first column of ‘B’.