生成在Matlab加权随机布尔矩阵 [英] Generate Weighted Random Boolean Matrix in Matlab

查看:437
本文介绍了生成在Matlab加权随机布尔矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要生成一个 5×5 矩阵MatLab的是仅由1和0的,但是我希望能够设置任何值,是一个1。我希望这是的可能性有90%的几率任何元素将是1,而不是0。

I want to generate a 5x5 matrix in MatLab that is made of only 1s and 0s, however I want to be able to set the likelihood of any value being a 1. I want there to be a 90% chance any element will be a 1 rather than a 0.

不过,我不想在那里只是永远是90%1和10%0。我只是想这是更可能发生的情况。例如。我还是希望它能够为全0,或50%1和50%0,只是不太可能。

However I do NOT want there to just always be 90% 1s and 10% 0s. I just want this to be the more likely case. E.g. I still want it to be possible to be all 0s, or 50% 1s and 50% 0s, just very unlikely.

感谢您的任何帮助。

推荐答案

我已经写的很清楚,简单的code明白了:

I've wrote a very clear and simple code to understand:

mat5x5 = rand(5);
chanse_of_1s=.90% probability for 1s, just change it.
chanse_of_0s=.10% probability for 0s, just change it.
mat5x5 = arrayfun(@(x)sum(x >= cumsum([0, chanse_of_0s, chanse_of_1s])), mat5x5)-1

只是恰克的chanses!

just chage the chanses!

这篇关于生成在Matlab加权随机布尔矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆