我要生成一个 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!