I am stuck at a problem in matlab. How do I select a number ‘i’ with a probability ‘i’ in a matrix.
I want to do this in a matrix, where I select a number ‘i’ from each row, with probability ‘i’.
Any help is appreciated
a sample matrix( dont need to select zeros):
w1= .47;
w2= .023;
m1= .06;
m2= .037;
x=rand(1,m1)<=m1;
tolerance= 0.01;
Transition=[m1 w1 0 w1 0 0 0;
0 m1 w1 0 0 0 0;
0 0 m1 w1 0 0 0;
0 0 0 m2 w2 0 0;
0 0 0 0 m2 w1 w1;
0 0 0 0 0 m1 w1
0 0 0 0 0 0 m1];
First, we could calculate a matrix of cumulative probabilities
Tcin preparation for a strategy that finds the first column exceeding a random value between 0 and 1.To now draw numbers from each row, we first draw
p = rand(size(T, 2), 1)and then find the column for whichpfalls into a bucket of cumulative probability:This works because order does not matter. For example, for some p = 0.2, some arbitrary cumulative distribution could increase from 0.65 to 0.85. The probability for
randto yield a value in this interval is indeed 0.2. If this is the last non-zero entry ofT,randwill return a value with 0.15 probability for which no number is drawn. In another example, if all entries inTare zero, the cumulative distribution will never exceed what can be drawn byrand. For a last example, if there are two entries at 0.5, each, the cumulative distribution will exceed what’s drawn byrandhalf and half at either column.