How to create a random Bernoulli matrix ?

129 vues (au cours des 30 derniers jours)
Mohab Mostafa
Mohab Mostafa le 6 Oct 2015
Commenté : Weijian Liu le 4 Juin 2022
I want to create a 256x256 random Bernoulli matrix, how to do that in matlab ?
  1 commentaire
Bilal Siddiqui
Bilal Siddiqui le 2 Oct 2018
It's simple. A Bernoulli trial produces one of only two outcomes (say 0 or 1). You can use binord. For example p=0.2; n=256; A=binornd(1,p*ones(n));
produces an nxn array of Bernoulli trials which are either 0 or 1 in each outcome. Hope this answers your question.

Connectez-vous pour commenter.

Réponse acceptée

Robert Dylans
Robert Dylans le 7 Oct 2015
Modifié(e) : Robert Dylans le 7 Oct 2015
p=0.5; %probability of success
n=256;
A=rand(n);
A=(A<p)
or
p=0.5;
A=(rand(256)<p)
  5 commentaires
Walter Roberson
Walter Roberson le 7 Oct 2015
If you have a matrix A of 0 and 1 then you can make it a matrix of -1 and +1 by using
A*2 - 1

Connectez-vous pour commenter.

Plus de réponses (2)

Byron McMullen
Byron McMullen le 15 Fév 2021
I use this.
bernoulli_matrix = randsrc(rows,cols,[-1,1]);
  1 commentaire
Weijian Liu
Weijian Liu le 4 Juin 2022
In my opinion, this is the best one.

Connectez-vous pour commenter.


Bilal Siddiqui
Bilal Siddiqui le 2 Oct 2018
It's simple. A Bernoulli trial produces one of only two outcomes (say 0 or 1). You can use binord. For example p=0.2; n=256; A=binornd(1,p*ones(n));
produces an 256x256 array of Bernoulli trials which are either 0 or 1 in each outcome. Hope this answers your question.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by