Grouping or categorizing data based on conditions

3 次查看(过去 30 天)
Dear all,
In my calculations data is generated as attached which is x,y,z and a value as 4th dimension.
I wanted to divide this data into goups based on their value for x,y and z and assign one index to each category and finally find how many points I have in each group. It's a kind of dividing a region into voxels for 3d or pixels in 2d.
For example, if 20 < x < 19 & 20 < y< 19 & 20 <z<19 => set 1 index for the data which are located in this category.
I found these functions but don't know which one is approporiate for my purpose as the data is huge; findgroups,categories(A) ,[X,Y,Z] = meshgrid(x,y,z), Y = discretize(X,edges).
Thank you in advance.
data = readtable ('Outpu.txt');
data = table2array(data);
x=data(:,1);
y=data(:,2);
z=data(:,3);
t=data(:,4);
figure
scatter3(x,y,z,2,t,'filled')
ax = gca;
ax.XDir = 'reverse';
zlim([-200 200])
xlim([-100 100])
ylim([-100 100])
view(-31,14)
xlabel('X')
ylabel('Y')
zlabel('Z')
cb = colorbar;
clim([0 20])
grid minor
  3 个评论
Hamid
Hamid 2022-7-27
@KSSV Thank you so much for your reply.
As you see in the plot, I wanted to divide all the region showed in the graph based on x and y and z into sub-regions (voxels) and set one index to each. Finally, calculate the number of points and average of t in each voxel.
I hope the following figure make my question clear.

请先登录,再进行评论。

采纳的回答

Hamid
Hamid 2022-8-10
for i = 1:length(xedges)-1
for j = 1:length(yedges)-1
for k = 1:length(zedges)-1
idx = ((xedges(i)<= x_p) & (x_p < xedges(i+1))) & ((yedges(j)<= y_p) & (y_p < yedges(j+1))) & ((zedges(k)<= z_p) & (z_p < zedges(k+1))) ;
G(i,j,k) = nnz(idx);
end
end
end

更多回答(1 个)

KSSV
KSSV 2022-7-27
How about this?
data = readtable ('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1078835/Outpu.txt');
data = table2array(data);
x=data(:,1);
y=data(:,2);
z=data(:,3);
t=data(:,4);
x0 = linspace(-40,40,5) ;
y0 = linspace(-40,40,5) ;
z0 = linspace(-200,200,10) ;
G = NaN(length(x),1) ;
count = 0;
for i = 1:length(x0)-1
for j = 1:length(y0)-1
for k = 1:length(z0)-1
count = count+1 ;
idx = ((x0(i)<= x) & (x < x0(i+1))) & ((y0(j)<= y) & (y < y0(j+1))) & ((z0(k)<= z) & (z < z0(k+1))) ;
G(idx) = count ;
end
end
end
%% Remove the points
x(isnan(G)) = [];
y(isnan(G)) = [];
z(isnan(G)) = [];
G(isnan(G)) = [] ;
figure
scatter3(x,y,z,2,G,'filled')
colormap(turbo)
  5 个评论
KSSV
KSSV 2022-7-28
You can get the count from G also. nnz(G==1) gives the number of points in the first cube etc.
Hamid
Hamid 2022-7-29
It returns 0 again. Could you please take a look at the code again? Thank you in advance.
data = readtable ('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1078835/Outpu.txt');
data = table2array(data);
x=data(:,1);
y=data(:,2);
z=data(:,3);
t=data(:,4);
x0 = linspace(-40,40,5) ;
y0 = linspace(-40,40,5) ;
z0 = linspace(-200,200,10) ;
G = NaN(length(x),1) ;
count = 0;
for i = 1:length(x0)-1
for j = 1:length(y0)-1
for k = 1:length(z0)-1
count = count+1 ;
idx = ((x0(i)<= x) & (x < x0(i+1))) & ((y0(j)<= y) & (y < y0(j+1))) & ((z0(k)<= z) & (z < z0(k+1))) ;
G(idx) = count ;
cnt = nnz(G==1) ; %%%%% I added this line but it returns zero.
end
end
end
%% Remove the points
x(isnan(G)) = [];
y(isnan(G)) = [];
z(isnan(G)) = [];
G(isnan(G)) = [] ;
figure
scatter3(x,y,z,2,G,'filled')
colormap(turbo)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by