필터 지우기
필터 지우기

Anybody help me by providing MatLab code for the three-dimensional histograms.

조회 수: 10 (최근 30일)
I have three-dimensional data such as X=(100*1) , Y=(100*1) , Z=(100*1). I want to display three dimensional histograms for the (100*3).
Please help me.
  댓글 수: 4
sharmin sathi
sharmin sathi 2021년 8월 5일
Hello KSSV,
hist3 is not for three dimensional histograms. It takes 2D data and represent in 3D mode.
thanks.
sharmin sathi
sharmin sathi 2021년 12월 13일
편집: sharmin sathi 2021년 12월 14일
Dear Walter Roberson sir,
I have N samples in four categories. I have attached data file for your consideration.
could you draw 4-D histogram and send code?
thanks in advance.

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

답변 (4개)

Walter Roberson
Walter Roberson 2021년 12월 15일
Sorry, No, I do not know how to draw 4 dimensional histograms.
You can quantize the X, Y, and Z coordinates, and you can count the number of entries per voxel, and you could do that independent for each of the categories. You could end up with a 3D array of counts for each of the 4 categorize. But in order to draw a histogram, you need an extra dimension. A histogram of 1D data requires two spatial dimensions to draw (X = category, Y = count). A histogram of 2D data requries three spatial dimensions to draw (X = first category, Y = second category, Z = count). A histogram of 3D data would require four spatial dimensions to draw (X, Y, Z, count).
Unfortunately, hardware that is able to draw in 4 spatial dimensions is extremely rare. So rare that I have never heard of any. The limit I have ever heard of was equipment that could, to a limited extent, work in 3 spatial dimensions and animate over time (but it was not able to handle holes.)
If you have three spatial dimensions and a value at each location, then there are some things you can do with representing different values by different colors and using volume rendering with some transparency. These are not histograms, though.
  댓글 수: 2
sharmin sathi
sharmin sathi 2021년 12월 17일
편집: sharmin sathi 2021년 12월 17일
Dear Walter Roberson sir,
thank you for your comments.
Actually i have 3-dimensional data X, Y , Z and four groups label (Four label) of this data. I have attached data file for your consideration.
How can i draw 3D histogram for multi-groups of data where X = first category, Y = second category, Z = third category.
Please could draw 3D histogram by following my data?
thanks in advance
Walter Roberson
Walter Roberson 2021년 12월 17일
You cannot draw a histogram of 3D data unless you have an output device which is able to operate in 4 spatial dimensions (something not known to exist), or else you allow one of the spatial dimensions to be represented by time.

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


Image Analyst
Image Analyst 2021년 12월 15일
With the poster's "X=(100*1) , Y=(100*1) , Z=(100*1)" you can get a 3-D histogram. Or you can get 3 1-D histograms. Or you can get a 3-D histogram like this:
h3 = zeros(length(X), length(Y), length(Y));
for k = 1 : length(X)
% Increment count by 1.
h3(X(k), Y(k), Z(k)) = h3(X(k), Y(k), Z(k)) + 1;
end
assuming the values of X, Y, and Z were already bin numbers. Otherwise you'd have to convert them to bin numbers using rescale() and round().
What you want to do really depends on what you want. What are you going to do with this/these histograms?
  댓글 수: 6
Walter Roberson
Walter Roberson 2021년 12월 17일
You have a 10 x 10 x 10 array of values. That is a cube of values.
You want to represent each value by a height. That is inherent in histogram: a histogram represents different counts by different heights.
So you have a cube of values, and at each location in the cube, you want a height.
Those heights would have to point in a 4th direction. However, no-one knows how to extend into a 4th spatial dimension. Scientists are not even sure if a 4th spatial dimension exists (but some parts of physics are a lot easier to explain if at least one more spatial dimension exists.)
Image Analyst
Image Analyst 2021년 12월 17일
Prepare to have your mind blown:
@sharmin sathi, like we've said before you can visualize a 4-D histogram by changing the size and/or color of the markers using scatter3.

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


sharmin sathi
sharmin sathi 2021년 12월 20일
Thank you all for nice cooperation
  댓글 수: 2
sharmin sathi
sharmin sathi 2021년 12월 28일
편집: Walter Roberson 2021년 12월 28일
I have attached two files one is input file and other is out file.
Pls could you tell me this graph is 4D or 3D graph. I have drawn this bar graph by using bar3 command.
fileName = 'data_with_group.csv';
data = readmatrix(fileName)
x=data(:,1);
y=data(:,2);
z=data(:,3);
label=data(:,4);
group = cell2mat(label);
uniqueGroups = unique(group);
for k = 1:length(uniqueGroups)
ind = group==uniqueGroups(k);
if k==1
bar3(x(ind),y(ind),z(ind),'b');
elseif k==2
bar3(x(ind),y(ind),z(ind),'g');
elseif k==3
bar3(x(ind),y(ind),z(ind),'c');
elseif k==4
bar3(x(ind),y(ind),z(ind),'r');
end
end
Walter Roberson
Walter Roberson 2021년 12월 28일
You cannot pass x, y, z values to bar3(). Your z values are being interpreted as bar width and your x values are being intepreted as y and your y values are being interpreted as z.

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


Walter Roberson
Walter Roberson 2021년 12월 28일
I am having trouble getting the coloring working, but this is what I have so far.
This code voxelizes the data (which is something histograms have to do). It draws in each occupied voxel.
For each voxel, it figures out which group has the greatest number of points within the voxel. It tries to color the voxel according to that label. The calculation of which label to use appears to be correct for the voxels, but I am having difficulty in getting patch to display anything other than black faces.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/845690/data_with_group.csv';
labelcolors = [
0 0 1
0 1 0
0 1 1
1 0 0]; %must be rgb
data = readmatrix(filename);
x = data(:, 1);
y = data(:, 2);
z = data(:, 3);
label = data(:,4);
[groups, ID] = findgroups(label);
ngroups = length(ID);
if ngroups > size(labelcolors,1)
error('too many groups, provide more labelcolors');
end
Nx = 20; Ny = 20; Nz = 20;
xvox = (max(x)-min(x))/(Nx-1);
yvox = (max(y)-min(y))/(Ny-1);
zvox = (max(z)-min(z))/(Nz-1);
xidx = floor(x/xvox); xbin = xidx - min(xidx) + 1;
yidx = floor(y/yvox); ybin = yidx - min(yidx) + 1;
zidx = floor(z/zvox); zbin = zidx - min(zidx) + 1;
xq = xidx * xvox; yq = yidx * yvox; zq = zidx * zvox;
counts = accumarray([ybin(:), xbin(:), zbin(:)], 1, [Ny Nx Nz]);
occupied = counts > 0;
oind = find(occupied);
gcounts = cell(ngroups, 1);
for K = 1 : ngroups
mask = groups == K;
gcounts{K} = accumarray([ybin(mask), xbin(mask), zbin(mask)], 1, [Ny Nx Nz]);
end
gcounts4 = cat(4, gcounts{:});
[~, biggestgroupidx] = max(gcounts4, [], 4);
colidx = biggestgroupidx(occupied);
[ox, oy, oz] = ind2sub(size(occupied), oind);
oxq = (ox + min(xidx) - 1) * xvox;
oyq = (oy + min(yidx) - 1) * yvox;
ozq = (oz + min(zidx) - 1) * zvox;
noq = length(oxq);
rF = [1 2 3 4 1; 8 7 6 5 8; 1 4 6 7 1; 2 8 5 3 2; 1 7 8 2 1; 3 5 6 4 3]; %in closed form
rV = [0 0 0; 1 0 0; 1 0 1; 0 0 1; 1 1 1; 0 1 1; 0 1 0; 1 1 0] .* [xvox, yvox, zvox];
%noq = 2;
poxq = oxq(1:noq); poyq = oyq(1:noq); pozq = ozq(1:noq);
allF = repmat(rF(:,1:end-1), noq, 1) + 8*repelem((0:noq-1).', size(rF,1), 1);
allV = repelem([poxq, poyq, pozq], size(rV,1), 1) + repmat(rV, noq, 1);
[mappedV, ~, Vidx] = unique(allV, 'rows');
mappedF = Vidx(allF);
cdata = labelcolors(colidx,:);
p = patch('Faces', mappedF, 'Vertices', mappedV, 'FaceVertexCData', cdata, ...
'LineWidth', 0.1, 'EdgeColor', 'none', 'FaceAlpha', 0.1);
colormap(labelcolors);
xlabel('x'); ylabel('y'); zlabel('z');
view(3)
  댓글 수: 2
sharmin sathi
sharmin sathi 2021년 12월 29일
Dear Walter Roberson sir,
Thank you so much for your answer.
is it possible to show the color of each Voxel by groupwise?.
I have attached an output image of coloring like this.
please help me.
Thanks in advance.
Franz-Josef Siegemund
Franz-Josef Siegemund 2023년 3월 3일
I fixed the color issue by doing the following which repeats the elemts for each of the 6 faces of a cube.
cdata = labelcolors(colidx,:);
cdata = repelem(cdata,6,1);

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by