How to create a random three-dimensional matrix?

Is there a way to create a 3D matrix randomly? There are ways to create random 2D matrices using a function randint. Is there a built-in function?

eg. 4x4 matrix can be easily generated using the function randint. What if I want to create a 4x4x3 matrix?

+4
source share
2 answers

You can use randi(imax, size1, size2, size3)where imaxrefers to the maximum values ​​of random numbers (middle upper bound) and 1- lower bound. You can expand the argument sizeto sizeNwhatever you want.

This is an example of its use:

>> 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
+9

, , randi . randi(10,3,3,3)

randi(10,3,3,3)

ans(:,:,1) =

     9    10     3
    10     7     6
     2     1    10


ans(:,:,2) =

    10    10     2
     2     5     5
    10     9    10


ans(:,:,3) =

     8     1     7
    10     9     8
     7    10     8
+6

Source: https://habr.com/ru/post/1570879/


All Articles