Eliminate rows in a matrix that have matching & different values

6 vues (au cours des 30 derniers jours)
ZigzS
ZigzS le 1 Mai 2018
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?

Réponse acceptée

Akira Agata
Akira Agata le 2 Mai 2018
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,:) = [];

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by