count 3D points to construct 3D voxel plot

5 次查看(过去 30 天)
I am using MATLAB R2015a on OSX 10.10.5. I would like to count number of points in a 3D grid and plot the voxel color coded with the counts. However my code produced a voxel plot that is not consistent with a scattered plots of my data (attached). I have high voxel counts in regions with few data points. Could you point out my error? Thank!
load data
bins = 20;
xedge = linspace(min(data(:,1)),max(data(:,1))+1e14*eps,bins);
yedge = linspace(min(data(:,2)),max(data(:,2))+1e14*eps,bins);
zedge = linspace(min(data(:,3)),max(data(:,3))+1e14*eps,bins);
loc = zeros(size(data));
len = length(xedge)-1;
mid = zeros(len,3);
for i = 1:len
mid(i,1) = (xedge(i)+xedge(i+1))/2;
mid(i,2) = (yedge(i)+yedge(i+1))/2;
mid(i,3) = (zedge(i)+zedge(i+1))/2;
end
[~,loc(:,1)] = histc(data(:,1),xedge);
[~,loc(:,2)] = histc(data(:,2),yedge);
[~,loc(:,3)] = histc(data(:,3),zedge);
hasdata = all(loc>0,2);
sz(1:3) = len;
cnt = accumarray(loc(hasdata,:),1,sz);
figure
subplot(1,2,1)
scatter3(data(:,1),data(:,2),data(:,3))
view(3)
subplot(1,2,2)
[x,y,z] = meshgrid(mid(:,1),mid(:,2),mid(:,3));
[~,I] = max(cnt(:));
[xi,yi,zi] = ind2sub(size(cnt),I);
slice(x,y,z,cnt,mid(xi,1),mid(yi,2),mid(zi,3),'nearest')
view(3)

采纳的回答

Walter Roberson
Walter Roberson 2015-9-12
Your code appears to be well designed.
What I notice when I test with your code is that the slice output appears to be the mirror of the scatter output along the y axis. I am not sure why that is at the moment, but try
slice(x,y,z,permute(cnt,[2 1 3]),mid(xi,1),mid(yi,2),mid(zi,3),'nearest')
If that works for you then the explanation would be rooted in the fact that for matrices, x is the columns and y is the rows

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by