Visualize a three-dimensional array like cubic lattice using MATLAB

后端 未结 1 1783
闹比i
闹比i 2020-12-11 06:17

I want to visualize a three-dimensional array just like cubic lattice using MATLAB.

I have read How to plot 3D grid (cube) in Matlab, and Simple cubic lattice using

1条回答
  •  再見小時候
    2020-12-11 07:09

    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.

    Result:

    enter image description here

    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)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');
    

    Function drawCube is as follows:

    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
    

    0 讨论(0)
提交回复
热议问题