How to create a random 3D matrix?

后端 未结 2 561
暗喜
暗喜 2021-01-18 15:46

Is there any way of creating a 3D matrix randomly? There are ways to create random 2D matrices using randint function. Is there any inbuilt function like that?

E.g.

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-18 16:19

    You can use randi(imax, size1, size2, size3) function where imax refers to maximum of random integer values (mean upper bound) and 1 is lower bound. You can expand size argument to sizeN what you want.

    This is an example of its usage:

    >> A = randi(5, 4, 4, 3)
    
    A(:,:,1) =
    
         4     4     5     4
         4     1     2     2
         2     1     3     3
         4     3     2     4
    
    
    A(:,:,2) =
    
         5     1     5     1
         5     2     2     2
         3     5     5     4
         1     2     2     3
    
    
    A(:,:,3) =
    
         2     5     2     3
         5     2     3     4
         3     4     1     5
         3     4     1     1
    

提交回复
热议问题