Create a 3D matrix

前端 未结 3 1916
有刺的猬
有刺的猬 2020-12-17 07:34

How can I define a 3D matrix in MATLAB?

For example a matrix of size (8 x 4 x 20) or add a 3rd dimension to an existing 2D matrix?

3条回答
  •  天涯浪人
    2020-12-17 08:23

    I use Octave, but Matlab has the same syntax.

    Create 3d matrix:

    octave:3> m = ones(2,3,2)
    m =
    
    ans(:,:,1) =
    
       1   1   1
       1   1   1
    
    ans(:,:,2) =
    
       1   1   1
       1   1   1
    

    Now, say I have a 2D matrix that I want to expand in a new dimension:

    octave:4> Two_D = ones(2,3)
    Two_D =
       1   1   1
       1   1   1
    

    I can expand it by creating a 3D matrix, setting the first 2D in it to my old (here I have size two of the third dimension):

    octave:11> Three_D = zeros(2,3,2)
    Three_D =
    
    ans(:,:,1) =
    
       0   0   0
       0   0   0
    
    ans(:,:,2) =
    
       0   0   0
       0   0   0
    
    
    
    octave:12> Three_D(:,:,1) = Two_D
    Three_D =
    
    ans(:,:,1) =
    
       1   1   1
       1   1   1
    
    ans(:,:,2) =
    
       0   0   0
       0   0   0
    

提交回复
热议问题