Pregunta

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.

¿Fue útil?

Solución

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!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top