MATLAB: Eliminate rows in a matrix that have matching & different values

remove row column unique minimum

I have a matrix that looks like this:
2.0000 15.0000 1.0000 11.2427
2.0000 15.0000 1.0000 15.9927
4.0000 15.0000 1.0000 17.4080
5.0000 15.0000 1.0000 11.8038
6.0000 15.0000 1.0000 11.5288
7.0000 15.0000 1.0000 11.3480
8.0000 15.0000 1.0000 11.1031
9.0000 15.0000 1.0000 11.2669
11.0000 15.0000 1.0000 11.4250
11.0000 15.0000 1.0000 11.8218
12.0000 15.0000 1.0000 11.3909
And I want to remove entire rows on two conditions:
1. Two rows have the same value in the first column (e.g. 2.0000=2.0000, 11.0000=11.0000)
2. The row with the larger value in the fourth column is removed (e.g. 11.2427 < 15.9927, 11.4250 < 11.8218)
So in the matrix above rows 2 and 10 would be removed, leaving me with
2.0000 15.0000 1.0000 11.2427
4.0000 15.0000 1.0000 17.4080
5.0000 15.0000 1.0000 11.8038
6.0000 15.0000 1.0000 11.5288
7.0000 15.0000 1.0000 11.3480
8.0000 15.0000 1.0000 11.1031
9.0000 15.0000 1.0000 11.2669
11.0000 15.0000 1.0000 11.4250
12.0000 15.0000 1.0000 11.3909
Such that the first column has no duplicates – it is totally unique. Any ideas?

Best Answer

Assuming your matrix is A, the following code returns what you want as B.
B = sortrows(A,[1 4]);
idx = [false; diff(B(:,1)) == 0];
B(idx,:) = [];