MATLAB: Max repetitive values in a vector

maximum of repetitive values

Hi all, I have problem with a code I am working on. I have a set of data; assume
A= [ 2 3 3 4;
4 4 3 6;
5 3 3 9;
2 4 3 8;
8 3 3 12;
1 1 1 2;
8 1 1 3];
I am trying to get the row which has same A(i,2)==A(i+1,2) and A(i,3)==A(i+1,3) based on the maximum value on the last element as A(i,4) and save it in a new set data. this is my code :
if A(i,2)==A(i+1,2)&& A(i,3)==A(i+1,3)
VALUE=max(beam_new(i:i+1,4));
A1(i,:)=A(i,:);
end
The problem is only my code compares each consecutive two rows not all the rows having the same values in the second and third columns.
For example the results would be:
A1= [8 3 3 12;
2 4 3 8;
8 1 1 3];
The picture shows the data with duplicated values in rows. I need just to extract thew rows based on the maximum values of element number A(i,4) for each duplicated data sets.
Any suggestions??
Thank you

Best Answer

I think this does what you want
A= [ 2 3 3 4;
4 4 3 6;
5 3 3 9;
2 4 3 8;
8 3 3 12;
1 1 1 2;
8 1 1 3];
[~,j,k] = unique(A(:,[2 3]),'row','stable');
num_A1_rows = numel(j);
A1 = zeros(num_A1_rows,4);
for na = 1:num_A1_rows
tmp = sortrows(A(k==na,:),4);
A1(na,:) = tmp(end,:);
end