Generate Weighted Random Boolean Matrix in Matlab

一笑奈何 提交于 2019-12-13 16:15:12

问题


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.

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.

Thank you for any help.


回答1:


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

just chage the chanses!



来源:https://stackoverflow.com/questions/18370382/generate-weighted-random-boolean-matrix-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!