How to generate a matrix with assigned probability?

8 ビュー (過去 30 日間)
Aryan Jain
Aryan Jain 2020 年 9 月 13 日
コメント済み: Aryan Jain 2020 年 9 月 13 日
Hi,
I want to generate a matrix of size (m,n) that has 1's and 0's, where each column has at random x% 1's and about (100-x)% 0. after every execution the matrix should change randomly.
For ex - suppose matrix A is a (5,2) matrix and x=0.8 i:e 80% are 1
then A should be
A=[1,1,1,0,1;1,0,1,1,1] % 1st itration
A=A=[0,1,1,1,1;1,1,1,0,1] % 2nd itration
How can I do it? Is there any way to generate it without using a for loop?
Thank You

採用された回答

Bruno Luong
Bruno Luong 2020 年 9 月 13 日
it doesn't matter whereas it run in for-loop or not.
x = 0.8; % probability of 1s
m = 5;
n = 2;
% for ...
A = rand(m,n) < x;
% end
  1 件のコメント
Aryan Jain
Aryan Jain 2020 年 9 月 13 日
Thank You very much.

サインインしてコメントする。

その他の回答 (1 件)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 9 月 13 日
% x is the probability of 1
% m rows and n columns
A=binornd(1,x,n,m)';
% another random matrix
A=binornd(1,x,n,m)';
  1 件のコメント
Aryan Jain
Aryan Jain 2020 年 9 月 13 日
Thank You very much sir.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by