How do I make different groups within a matrix?
显示 更早的评论
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 个评论
Mathieu NOE
2021-3-12
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
2021-3-12
Adam Danz
2021-3-12
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);
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
