# randsample([0 1],1,true,[1 - p p]) ..... with p as a vector?

2 Ansichten (letzte 30 Tage)
Ulrik William Nash am 29 Nov. 2018
Beantwortet: Bruno Luong am 29 Nov. 2018
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 KommentareKeine anzeigenKeine ausblenden
Rik am 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 am 29 Nov. 2018
p in the code is a single value. I am asking if this function can handle multiple values of p at once, thus generating a vector of 0's and 1's corresponding to a vector of p values.

Melden Sie sich an, um zu kommentieren.

### Antworten (2)

Walter Roberson am 29 Nov. 2018
Bearbeitet: Walter Roberson am 29 Nov. 2018
double( rand(size(p)) < p )
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Bruno Luong am 29 Nov. 2018
Assuming yoy have p in a vector
r = rand(size(p))<p
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Nearest Neighbors finden Sie in Help Center und File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by