# Grouping or categorizing data based on conditions

4 views (last 30 days)
Hamid on 27 Jul 2022
Answered: Hamid on 10 Aug 2022
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
Chunru on 27 Jul 2022

Hamid on 10 Aug 2022
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

### More Answers (1)

KSSV on 27 Jul 2022
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)
Hamid on 29 Jul 2022
It returns 0 again. Could you please take a look at the code again? Thank you in advance.
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)

### Categories

Find more on Matrices and Arrays in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!