MATLAB: Remove duplicate elements in an Array based on one of the columns

arrayduplicate elements

Hi , I have a matrix 5000×4 in size. It has rows which have the identical elements in the 1st and 2nd column but different elements in the 4th column. I would like to be able to remove the elements where the 1st and 2nd columns are repeated , and Have a smaller value in the corresponding 4th column.
A =[1 2 3 4;
1 2 4 6;
3 5 7 2;
3 5 4 5;
3 5 3 7;]
In this example , i would like to remove the 1st row and the 3rd and 4th rows too. The 4h column of these rows are lesser than the competing rows' 4th column. I hope its not too confusing.
Thanks, Appreciate the help !

Best Answer

Here is one way to do it:
A =[1 2 3 4;
1 2 4 6;
3 5 7 2;
3 5 4 5;
3 5 3 7;]
[~, ~, rows] = unique(A(:, [1 2]), 'rows');
todelete = arrayfun(@(r) rows == r & A(:, 4) < max(A(rows == r, 4)), 1:max(rows), 'UniformOutput', false);
A(any(cell2mat(todelete), 2), :) = []