How do I make different groups within a matrix?
Ältere Kommentare anzeigen
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 Kommentare
Mathieu NOE
am 12 Mär. 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
am 12 Mär. 2021
Adam Danz
am 12 Mär. 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);
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
