randsample([0 1],1,true,[1 - p p]) ..... with p as a vector?
Show older comments
I have the following code that generates a 0 or a 1 depending on the probability p:
randsample([0 1],1,true,[1 - p p])
But suppose my probability was taken from a vector of other probabilities. Is it possible to ammend my code to handle all probabilities at once, rather than looping through the vector for each probability?
2 Comments
Rik
on 29 Nov 2018
I don't understand your question, nor do I have the toolbox for this function, so I can't play around with it for you.
Ulrik William Nash
on 29 Nov 2018
Answers (2)
Walter Roberson
on 29 Nov 2018
Edited: Walter Roberson
on 29 Nov 2018
double( rand(size(p)) < p )
Bruno Luong
on 29 Nov 2018
Assuming yoy have p in a vector
r = rand(size(p))<p
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!