bucketing values

Hi
I have a vector of values from 0 to 1 representing probabilities
A = [0.8756 0.1185 0.0059]
How can i generate a random number using rand() and map it to a value in A without looping. I can;t see how i can do this with vectorisation or with the built in functions.
My aim is generate a random numbers so 87% of the time I choose A(1), 11% A(2), etc...
Thanks

 채택된 답변

Teja Muppirala
Teja Muppirala 2011년 7월 21일

0 개 추천

If you have the Statistics Toolbox installed, there is the built-in RANDSAMPLE command to do this.
A = [0.8756 0.1185 0.0059];
X = randsample(numel(A),5000,true, A); % X gets 5000 samples
tabulate(X)

추가 답변 (5개)

Daniel Shub
Daniel Shub 2011년 7월 21일

0 개 추천

Assuming your array A is short ...
A = [0.8756 0.1185 0.0059]
x = rand(1e3, 1);
y(x < A(1)) = 'a';
y(x >= A(1) & x < A(2)) = 'b';
y(x >= A(2)) = 'c';

댓글 수: 2

Fangjun Jiang
Fangjun Jiang 2011년 7월 21일
I think all the answers provided are along the same line. In your case, the A needs to be [0.87,0.98,1].
Daniel Shub
Daniel Shub 2011년 7월 21일
You are correct. I was sloppy.

댓글을 달려면 로그인하십시오.

Sean de Wolski
Sean de Wolski 2011년 7월 21일

0 개 추천

Like this?
A = [0.8756 0.1185 0.0059]; %percentiles (must sum to 1
R = rand(1,1000); %random data
B = [11 17 19]; %sample data to extract A% of the time
[junk,Bin] = histc(R,[-inf cumsum(A)]); %find the bin
C = B(Bin) %extract
Fangjun Jiang
Fangjun Jiang 2011년 7월 21일

0 개 추천

Something like this:
A = [0.8756 0.1185 0.0059];
RandNum=rand(1000,1);
Index=2*ones(size(RandNum));
Index(RandNum<A(1))=1;
Index(RandNum>1-A(3))=3;
sum(Index==1)
sum(Index==2)
sum(Index==3)
ans =
892
ans =
102
ans =
6
close enough?
Greg
Greg 2011년 7월 21일

0 개 추천

Thanks for all the answser
I'm not sure which idea scales better as in general A[] will be of different sizes so I can't explicitly code for 3 items.
I'm fairly new to Matlab so any further pointers is much appreciated.
Thanks

댓글 수: 1

Sean de Wolski
Sean de Wolski 2011년 7월 21일
Randsample and Histc are both automated for large/varying A.

댓글을 달려면 로그인하십시오.

Greg
Greg 2011년 7월 21일

0 개 추천

Thanks Teja
I have the Stats Toolbox so this looks to be exactly what i'm looking for. I will play around with this some more but so far so good
Cheers Greg

제품

질문:

2011년 7월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by