MATLAB: How can one do this calculation

MATLAB

In a previous post, I have created a matrix
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
2 4
2 5
For each 2D grid point I calculate some number in the third column
1 1 10
1 2 39
1 3 24
1 4 7
1 5 41
2 1 69
2 2 73
2 3 121
2 4 10
2 5 7
Then what I would like to do is to (1) choose the max of numbers in column 3 whose column 1 number is 1, 2, 3, 4,… and (2) also record the number of the second column for a row that the column 3 number is max. That is, I would like to create a matrix
1 5 41
2 3 121
3 ..
4 ..
5 ..
Please advise.

Best Answer

clearvars;
clc;
A = [1 1 10
1 2 39
1 3 24
1 4 7
1 5 41
2 1 69
2 2 73
2 3 121
2 4 10
2 5 7
3 1 9
3 2 15
3 3 100
3 4 131
3 5 133];
A_New = zeros(max(A(:,1)), 3);
for ii = 1 : 1 : max(A(:,1))
[Val,Ind] = max(A(5*(ii-1)+1:5*ii,3));
A_New (ii,1) = ii;
A_New (ii,2) = A(Ind,2);
A_New (ii,3) = Val;
end