chooseinteger.m

来自「The package includes 3 Matlab-interfaces」· M 代码 · 共 22 行

M
22
字号
% talyamfunction i = chooseInteger(q)% i = chooseInteger(q)% given a row vector q (1XN), return i=1..N in probabilty q(i).% for this purpose, I use rand to choose a number x, uniformly% distributed on the interval (0.0, 1.0), and pick i in the% following way:% i = 1 if x<q(1)                      ==> Pr(1) = q(1)% i = 2 if x>q(1) and x<(q(1)+q(2))    ==> Pr(2) = q(2)%               ...                         ...% i = N if x>(q(1)+...+q(N-1))         ==> Pr(N) = q(N)x = rand;N = length(q(1,:));bins = cumsum(q);i = 0;for j=1:N    if (x<bins(1,j))        i = j;        break;    endend

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?