How do I make different groups within a matrix?
Afficher commentaires plus anciens
I have a 2904x3 matrix where each column represents the x, y and z coordinates of some vectors. Some of these vectors have the same z-coordinate and I need to group those together. Any ideas?
3 commentaires
Mathieu NOE
le 12 Mar 2021
hello
have you tried with unique ?
a = [1 1 1 2 2 2 3 3 4 4];
[C,IA,IC] = unique(a);
C =
1 2 3 4
IA =
1
4
7
9
IC =
1
1
1
2
2
2
3
3
4
4
Jaime Castiblanques
le 12 Mar 2021
Adam Danz
le 12 Mar 2021
Extending Mathieu NOE's suggestion, the 3rd output to unique is a grouping variable but you should use the stable flag to ensure that the grouping values correspond to each element of the vector.
% xyz is nx3 matrix of [x,y,z] values
[~,~,zgroup] = unique(xyz(:,3));
Alternatively, if you just want to sort the matrix according to the z column,
xyzSort = sortrows(xyz,3);
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
