How to generate a random matrix ?

how to generate (4,4) a 20 binary random matrix in one function? like this
x1 = [ 1 0 1 0
1 1 0 0
0 1 1 0
0 0 1 0 ]
x2 = [ 1 1 0 1
0 1 0 0
1 1 0 1
0 1 0 1 ]
.
.
.
.
x20 = [ 1 0 1 0
0 0 0 1
1 1 1 0
1 0 1 1 ]

Respuestas (3)

Image Analyst
Image Analyst el 17 de Abr. de 2016
Editada: Image Analyst el 17 de Abr. de 2016
Try this:
x1 = randi([0, 1], [4,4])
Repeat for all of the other x. Or else (better because it's possibly more convenient), make a 3D array of 4-by-4-by-20.
Note: x1 is a double. If you want a binary as a boolean or logical, cast to logical:
x1 = logical(randi([0, 1], [4,4]))

3 comentarios

To specify the size of a square matrix, it is only necessary to use one dimension:
x1 = randi([0, 1], 4)
The matrix-generating functions assume a square matrix.
Hirak Basumatary
Hirak Basumatary el 14 de Jun. de 2019
How to generate unique random matrices ? Any algo for this?
You can use isequal() to compare the new matrix against any prior matrix.
if ~isequal(testMatrix, existingMatrix)
% Generate new test matrix
end

Iniciar sesión para comentar.

Harshit Agarwal
Harshit Agarwal el 1 de Abr. de 2019

0 votos

theta = [2,1]

1 comentario

Image Analyst
Image Analyst el 1 de Abr. de 2019
That is not a 4-by-4 square. And, it is not binary. So it's not an answer to Firas's question.

Iniciar sesión para comentar.

Jenin
Jenin el 31 de Oct. de 2022
Movida: Image Analyst el 1 de Nov. de 2022

0 votos

You can use x = rand(4,4,20), It will gives you 20 set of 4x4 matrices.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 17 de Abr. de 2016

Movida:

el 1 de Nov. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by