matlab中的4维图

时间:2016-03-07 17:52:30

标签: matlab multidimensional-array plot contour

我正在尝试使用我的4维数据在Matlab中创建一个图。数据看起来像[x,y,z,d],其中x,y和z是对象的坐标," d"是强度。我试图映射数据,只考虑[x,y,d]并进行轮廓或冲浪但是为了那个" d"应该是一个矩阵。我也使用了plot3,但它看起来不漂亮或不像轮廓。知道如何以一种很好的方式绘制这个吗? 以下是数据示例:

  1.2500000E-02  1.2500000E-02  1.2500000E-02  1.054474998136970E-002
  1.2500000E-02  1.2500000E-02  3.7500001E-02  1.348833743199940E-003
  1.2500000E-02  1.2500000E-02  6.2500000E-02  0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  8.7499999E-02  0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.1125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.1375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.1625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.1875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.2125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.2375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.2625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.2875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.3125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.3375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.3625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.3875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.4125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.4375000      7.452120128176466E-006
  1.2500000E-02  1.2500000E-02  0.4625000      2.235636038452940E-005
  1.2500000E-02  1.2500000E-02  0.4875000      4.471272076905880E-005
  1.2500000E-02  1.2500000E-02  0.5125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.5375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.5625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.5875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.6125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.6375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.6625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.6875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.7125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.7375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.7625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.7875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.8125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.8375000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.8625000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.8875000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.9125000      0.000000000000000E+000
  1.2500000E-02  1.2500000E-02  0.9375000      7.824726134585290E-004
  1.2500000E-02  1.2500000E-02  0.9625000      9.657947686116700E-003
  1.2500000E-02  1.2500000E-02  0.9875000      3.128400029808481E-002
  1.2500000E-02  3.7500001E-02  1.2500000E-02  1.028392577688352E-003

由于

1 个答案:

答案 0 :(得分:0)

假设我们可以使用[x,y,z,T]形式转换矩阵,以形成3个向量XYZ和3-D矩阵TT(ii,jj,kk) 1}}对应X(ii)Y(jj)Z(kk)

不可能按原样绘制这样的字段,最接近观察者的可视化数据将隐藏其余部分,我们可以绘制这样的字段的子集,让我们称之为 slice 尽管它不是肉,它必须是一架飞机。

在我的代码中假设切片被定义为函数z = f(x,y)。

在第一部分中,我定义了XYZ向量和字段T,其强度等于到[0 0 0]位置的距离。

然后我创建 slice 并使用封装函数getTvalue我计算适当的intesity。

  

getTvalue从网格中插入T(z)依赖关系并计算给定Z的值。

然后我使用surf绘制切片来显示几何体(前3个参数),第四个参数代表颜色 - 在我们的例子中是强度。

function[]=plot4d()
clc,close all
% Define the mesh
N=21;
X=-1:1/(N-1):1;
Y=X;
Z=X;
M=size(X,2);

% Define intensity matrix
T=zeros(M,M,M);
for ii=1:M
  for jj=1:M
    for kk=1:M
      T(ii,jj,kk)=sqrt((X(ii))^2+(Y(jj))^2+(Z(kk))^2);
    end
  end
end

for ii=1:M
  for jj=1:M
    SliceZ(ii,jj)=X(ii)+(Y(jj)^2);    % This is the SLICE FUNCTION, here (z=x+y^2)
% Calculate T value for (ii,jj) position using Z mesh, T values and actual Z position)
    SliceT(ii,jj)=getTvalue(ii,jj,Z,T,SliceZ(ii,jj));
  end
end

% Plot surface in range of X and Y, elevation defined by SliceZ and colour defined by SliceT without lines
surf(X,Y,SliceZ,SliceT,'linestyle','none')
% Plot the cross locating [0 0 0] point
line('xdata',[-1 1],'ydata',[0 0],'zdata',[0 0])
line('ydata',[-1 1],'xdata',[0 0],'zdata',[0 0])
line('zdata',[-1 1],'ydata',[0 0],'xdata',[0 0])
axis equal
% plot4d ends here

%% Nested functions:

function[Tvalue]=getTvalue(xi,yj,Zvector,Tmatrix,Zvalue)
% Test if the actual slice position (Zvalue) is inside the mesh (Zvector)
if Zvalue>max(Zvector)||Zvalue<min(Zvector)
  % If not, return NaN and process to next iteration
  Tvalue=nan;
  return
end

M=max(size(Zvector));                      % get the size of Z mesh
Index=find(Zvalue<=Zvector,1)              % find interval inside which the Zvalue is

% Test if the Zvalue is on the edge of mesh (Zvector)
if Index==M
  % Zvalue is on the edge - return the edge value (it prevents the index overflow)
  Tvalue=Tmatrix(xi,yj,M);
else
% Zvalue is inside the mesh (Zvector)

% calculate the slope of T(x,y,z)=k*z(x,y)+q dependence.
k=(Tmatrix(xi,yj,Index)-Tmatrix(xi,yj,Index+1))/(Zvector(Index)-Zvector(Index+1));

% calculate transposition of the dependence   
q=Tmatrix(xi,yj,Index)-k*(Zvector(Index)); 

% calculate the actual value of T
Tvalue=k*Zvalue+q; 
end
% End of nested function