Grouping or categorizing data based on conditions

조회 수: 5 (최근 30일)
Hamid
Hamid 2022년 7월 27일
답변: Hamid 2022년 8월 10일
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에서 Computational Geometry에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by