Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a random 3D matrix?

Tags:

matrix

matlab

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. a 4x4 matrix can be generated easily by using the randint function. What if I want to create a matrix of dimension 4x4x3?

like image 613
Abirami Anbalagan Avatar asked Jan 12 '15 13:01

Abirami Anbalagan


People also ask

Can a matrix be 3D?

A 3D matrix is nothing but a collection (or a stack) of many 2D matrices, just like how a 2D matrix is a collection/stack of many 1D vectors. So, matrix multiplication of 3D matrices involves multiple multiplications of 2D matrices, which eventually boils down to a dot product between their row/column vectors.

How do you generate a random matrix in Matlab?

You can use the randperm function to create a double array of random integer values that have no repeated values. For example, r4 = randperm(15,5);

How do you declare a 3D array in Python?

To create a three-dimensional array, we pass the object representing x by y by z in python, where x is the nested lists in the object, y is the nested lists inside the x nested lists, and z is the values inside each y nested list. The newly created three-dimensional array is stored in the variable called threedimarray.

How do you select a random number in a matrix?

Code for Picking a Random Array Value The code for picking a random value from an array looks as follows: let randomValue = myArray[Math. floor(Math. random() * myArray.


2 Answers

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
like image 75
mehmet Avatar answered Oct 13 '22 12:10

mehmet


If you read the help carefully, you will notice that the randi function accepts any number of dimensions. You may do 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
like image 23
Ander Biguri Avatar answered Oct 13 '22 11:10

Ander Biguri