Eliminate all non unique rows from a matrix
25 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
i've got a problem I don't really know how to solve efficiently:
I have a 2D pointcloud in matrix form: [x-values, y-values]. Matrix dimensions are nx2.
Now I want to make it so that every x- and every y-value only appear once in the entire matrix. So that for example if the 3rd point has x=2 every other point with x=2 is deleted and if the 1st point has y=7 every other point with y=7 is deleted. I could of course use unique(x) and unique(y) but then the results will most likely have different dimensions and also when I delete doublings of a certain x-value I need to delete the whole row so that the corresponding y-value is deleted, too and the other way round.
I hope my explanation was clear enough (not a native speaker) and someone can help me ;)
0 commentaires
Réponses (2)
the cyclist
le 18 Juin 2017
What would the output be for
x = [2 6;
2 7;
3 7]
If you mean that you want to keep all the rows, because they are all unique, just do
unique(x,'rows')
If not, then you need to clarify the rule. Keep the first row with the 2? Or the first row with the 7?
the cyclist
le 18 Juin 2017
Taking into account your comments in my other answer, I think this does what you want.
M = [2 6;
2 7;
3 7;
4 7];
% Remove non-unique first column values
[~,x_idx] = unique(M(:,1));
M2 = M(x_idx,:);
% Then remove non-unique second column values
[~,y_idx] = unique(M2(:,2));
M3 = M2(y_idx,:);
The output you need is in M3.
0 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!