group coordinates according to the value of z
2 views (last 30 days)
Show older comments
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1222387/image.png)
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
0 Comments
Accepted Answer
Askic V
on 7 Dec 2022
Edited: Askic V
on 7 Dec 2022
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 Comments
Askic V
on 7 Dec 2022
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
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!