问题描述
我想使用 MATLAB 将三维数组可视化,就像立方点阵一样.
I want to visualize a three-dimensional array just like cubic lattice using MATLAB.
我已阅读如何在 Matlab 中绘制 3D 网格(立方体) 和 使用三维数组的简单立方点阵
如果数组中的元素只有0和1,我知道如何使用三维数组绘制一个简单的立方点阵,小立方体的大小相同.
If element in the array is only 0 and 1, I know how to use a three-dimensional array to draw a simple cubic lattice, the small cube has the same size.
然而,现在我有一个像这样的三维数组,
However, Now I have a three-dimensional array like this,
cube(:,:,1) =
1 0 1
0 1 1
2 1 0
cube(:,:,2) =
0 0 1
1 5 1
0 1 0
cube(:,:,3) =
1 1 1
0 1 1
2 0 1
数组立方体有除0和1之外的值.我想把数组形象化为立方点阵,其中cube(:,:,1)表示立方点阵的第一层,
The array cube have value except 0 and 1. I want to visualize the array like cubic lattice, in which cube(:,:,1) denotes the first floor of the cubic lattice,
cube(:,:,2) denotes the second floor, and
cube(:,:,3) the third floor.
值 0 表示什么都没有,而值 1 表示一个小的蓝色立方体.
The value 0 denotes nothing, while value 1 denotes a small blue cube.
值大于 1 表示球体,球体的直径根据值而变化.想要的结果是这样的:
Value greater than 1 denotes sphere, the diameter of sphere varies according to the value. The desired result is something like this:
一个三维数组的可视化,1表示一个小绿色立方体,0 表示没有,值大于 1 表示白色球体,球体直径随数值变化.
为了更清楚地解释我的问题,展示一个二维数组的可视化
To explain my question more clear, show one visualization of two-dimensional array
1 表示一个小黑球,0 表示没有,值大于 1表示白色球体,球体的直径根据价值.
想要的效果图
可以,当立方体的边长为1时
it is Ok, when side length of cube is 1
当边长设置为 2 时,drawCube([ix,iy,iz],2,Royal_Blue).出现问题,多维数据集重叠,
when set side length as 2, drawCube([ix,iy,iz],2,Royal_Blue). The problem occurs, cubes is overlapping,
推荐答案
让我向您展示我的尝试.它基于独立绘制每个立方体和圆形.如果 A
很大,这会很慢.
Let me show you my attempt. It is based into drawing each cube and circle independently. This will be slow in if A
is big.
结果:
代码应该是不言自明的.
The code should be self explanatory.
% Create some data. This piece of code just creates some matrix A with
% some 1s and 0s and inserts a 2 and a 3 to specific positions. Subsitute
% this with your own data matrix.
th=0.2;
A=double(rand(10,10,10)<th);
A(1,1,1)=2;
A(5,5,5)=3;
% A nice color. I just dont like the standard blue so I picked another one.
Royal_Blue=[65 105 225]/255;
%%%%%%%%%%%%%%%%%%%%%%
%% Draw cubes
% Obtain all the linear indexes (search mathworks for help between
% subscripts vs linear indices) of the locations where a cube is wanted
% (A==1)
ind=find(A==1);
% Create a figure
fig=figure();
hold on
% Draw the cubes one by one
for ii=1:length(ind)
% For each linear index get its subscript (that also
% will be x,y,z position)
[ix,iy,iz]=ind2sub(size(A),ind(ii));
% Use the drawcube function to draw a single cube in the
% desired position with the desired size (1) and colour.
drawCube([ix,iy,iz],1,Royal_Blue);
end
% Nice plotting code. This just makes the drawing nicer.
camlight left
lighting gouraud
axis equal
axis off
view(-50,25)
%%%%%%%%%%%%%%%
%% Now draw the spheres
% This code is the same as the previous one but I just draw
% spheres instead of cubes.
ind=find(A>1);
% create an sphere
[X,Y,Z] = sphere;
for ii=1:length(ind)
[ix,iy,iz]=ind2sub(size(A),ind(ii));
% scale sphere
Xs=X*A(ix,iy,iz)/2;
Ys=Y*A(ix,iy,iz)/2;
Zs=Z*A(ix,iy,iz)/2;
surf(Xs+ix,Ys+iy,Zs+iz,'edgecolor','none','facecolor',[1 1 1]);
end
% Change the background colour to black
whitebg(fig,'k')
% MAke sure it stays black
set(gcf, 'InvertHardCopy', 'off');
函数drawCube
如下:
function drawCube( origin, size,color)
% From
% http://www.mathworks.com/matlabcentral/newsreader/view_thread/235581
if nargin<3
color='b';
end
x=([0 1 1 0 0 0;1 1 0 0 1 1;1 1 0 0 1 1;0 1 1 0 0 0]-0.5)*size+origin(1);
y=([0 0 1 1 0 0;0 1 1 0 0 0;0 1 1 0 1 1;0 0 1 1 1 1]-0.5)*size+origin(2);
z=([0 0 0 0 0 1;0 0 0 0 0 1;1 1 1 1 0 1;1 1 1 1 0 1]-0.5)*size+origin(3);
for i=1:6
h=patch(x(:,i),y(:,i),z(:,i),color);
set(h,'edgecolor','none')
end
end
这篇关于使用 MATLAB 可视化三次方格等三维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!