How do I make different groups within a matrix?
Show older comments
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 Comments
Mathieu NOE
on 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
on 12 Mar 2021
Adam Danz
on 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);
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!