group coordinates according to the value of z

2 次查看(过去 30 天)
Hi. Attached is the file "coordinates.txt".
I would like to group the coordinates of an mx3 array based on the value of z and place each group of values (with equal z) inside different cells.
The image shows what I would like to do.
I am starting like this. How can I modify it?
coordinate = importdata('coordinate.txt');
z_coordinate = coordinate(:,3);
for jj = 1:length(z_coordinate)
Group{jj} = z_coordinate(jj,:);
end

采纳的回答

Askic V
Askic V 2022-12-7
编辑:Askic V 2022-12-7
I would start like this:
coordinate = importdata('coordinate.txt');
z_coordinate = unique(coordinate(:,3));
N = numel(z_coordinate);
cell_group = cell([],N);
for ii = 1: N
ind = find(coordinate(:,3) == z_coordinate(ii));
cell_group{ii} = coordinate(ind,1:2);
end
cell_group{1}
This will produce 1x6 cell array:
cell_group =
1×6 cell array
{9×2 double} {6×2 double} {13×2 double} {11×2 double} {3×2 double} {11×2 double}
I'm sure there is even more efficient way to do this, but there are real gurus here who can help.
ans =
205 329
205 328
205 327
205 326
205 325
205 324
205 323
205 322
206 330
  2 个评论
Alberto Acri
Alberto Acri 2022-12-7
I thank you @Askic V! If I wanted to keep within each cell the z coordinates as well?
Askic V
Askic V 2022-12-7
in that case, the line
cell_group{ii} = coordinate(ind,1:2);
becomes
cell_group{ii} = coordinate(ind,:);
First element of cell_group is then:
ans =
205.0000 329.0000 5.9649
205.0000 328.0000 5.9649
205.0000 327.0000 5.9649
205.0000 326.0000 5.9649
205.0000 325.0000 5.9649
205.0000 324.0000 5.9649
205.0000 323.0000 5.9649
205.0000 322.0000 5.9649
206.0000 330.0000 5.9649

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by