Borrar filtros
Borrar filtros

Generating a random binary matrix

87 visualizaciones (últimos 30 días)
imed NASRI
imed NASRI el 6 de En. de 2014
Comentada: Jack Nelson el 13 de Mayo de 2020
Hello,
I want to generate randomly a (nxm) matrix of binary variables (0 and 1) in matlab. Is there someone who has a suggestion? Thanks.

Respuesta aceptada

Amit
Amit el 6 de En. de 2014
A = randi([0 1], n,m)
  2 comentarios
imed NASRI
imed NASRI el 6 de En. de 2014
Thanks
Jack Nelson
Jack Nelson el 13 de Mayo de 2020
Thanks!

Iniciar sesión para comentar.

Más respuestas (1)

Jos (10584)
Jos (10584) el 6 de En. de 2014
Some other suggestions
n = 4 ; m = 5 ;
A1 = rand(n,m) < 0.5 % a logical array consuming little memory
A2 = round(rand(n,m))
N0 = ceil(n*m/3) ; % specify some exact number of zeros
A3 = ones(n,m) ; A3(1:N0) = 0 ; A3(randperm(numel(A3))) = A3

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by