Eliminate rows in a matrix that have matching & different values
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ZigzS
el 1 de Mayo de 2018
Respondida: Akira Agata
el 2 de Mayo de 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?
0 comentarios
Respuesta aceptada
Akira Agata
el 2 de Mayo de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!