Need help creating an matrix that includes probability

1 回表示 (過去 30 日間)
Raul Castillo
Raul Castillo 2019 年 10 月 8 日
コメント済み: the cyclist 2019 年 10 月 8 日
So i am asked to make an matrix that is 96x96. this matrix must have a 5% probability of getting a 0 instead of a 1. i know that when creating the matrix i can write
matrix= ones (96,96)
i just dont know how to implement the random generating

採用された回答

the cyclist
the cyclist 2019 年 10 月 8 日
There are many ways to generate pseudorandom numbers in MATLAB. Since this sounds like homework, I'll just point you to this documentation page to get you started.
  3 件のコメント
Raul Castillo
Raul Castillo 2019 年 10 月 8 日
r = randi([0 1],96,96) so i found this way of doing it but it just randomly genereates zeros and ones. how can i control the random ability to be 5%?
the cyclist
the cyclist 2019 年 10 月 8 日
編集済み: the cyclist 2019 年 10 月 8 日
You can certainly do the task using randi. But you are correct that your syntax is not the way.
The syntax
randi(100,96,96)
will generate a 96x96 matrix of numbers, where each one is randomly drawn from 1 to 100.
Can you think of a way to then transform that matrix to one with 0's and 1's, where 95% are 1's (on average)?
You could also do a very simliar method using rand instead of randi.

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

その他の回答 (1 件)

Ajay Kumar
Ajay Kumar 2019 年 10 月 8 日
r = randi([0 1],96,96);
zero_count = round(0.05*numel(r));
count = 0;
for i = 1 : 96
for j = 1: 96
if r(i,j) == 0
count = count+1;
if count > zero_count
r(i,j) = 1;
else
end
end
end
end
  1 件のコメント
the cyclist
the cyclist 2019 年 10 月 8 日
I assume Raul meant that each matrix element has a 5% chance of being zero. This method will not yield that.

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

カテゴリ

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