Create a 3D matrix

前端 未结 3 1917
有刺的猬
有刺的猬 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:30

    If you want to define a 3D matrix containing all zeros, you write

    A = zeros(8,4,20);
    

    All ones uses ones, all NaN's uses NaN, all false uses false instead of zeros.

    If you have an existing 2D matrix, you can assign an element in the "3rd dimension" and the matrix is augmented to contain the new element. All other new matrix elements that have to be added to do that are set to zero.

    For example

    B = magic(3); %# creates a 3x3 magic square
    B(2,1,2) = 1; %# and you have a 3x3x2 array
    

提交回复
热议问题