Grouping or categorizing data based on conditions

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

You want to categorize based on t? This woul dbe more apt....or you are expecting a concentric cylinders? What output you are expecting? Any pictorial example?
@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 Thanks a lot and I appreciate taking your time. That was different form what I expected. I think, I need 3 values for indexing. Please see the graph attached. I used histcnd but I cannot use t values to find the avarage of t in each voxel.
@Chunru Many thanks for your comment. I used that but do you have any idea how to use the 4th data t? I need to find average value of t for each voxel.
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);
xedges=-100:10:100;
yedges=-100:10:100;
zedges=-100:10:100;
n=histcnd(x,y,z,xedges,yedges,zedges);
In the given code.....this line:
idx = ((x0(i)<= x) & (x < x0(i+1))) & ((y0(j)<= y) & (y < y0(j+1))) & ((z0(k)<= z) & (z < z0(k+1))) ;
gives you the logical indices of thew points lying inside the cube specified by (x0(i),x0(i+1)) etc... USe nnz(idx) to get the count of the points.
@KSSV Thanks a lot. I checked but nnz(idx) returns 0.
Is there any way that I can cosider the fourth variable t?
You can get the count from G also. nnz(G==1) gives the number of points in the first cube etc.
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)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 7월 27일

답변:

2022년 8월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by