Eliminate rows in a matrix that have matching & different values
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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?
0 Commenti
Risposta accettata
Akira Agata
il 2 Mag 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,:) = [];
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!