Question on cell arrays

2 views (last 30 days)
jana
jana on 16 Dec 2013
Commented: jana on 16 Dec 2013
Hi, I wanted to create a set of random numbers. I used the following code:
for i = 1:n
for j = 1:n
Sc{i,j} = sort((randperm(m,randi(m))));
end;
end;
This creates a cell array containing a set of random integers from 1 to 5. example Sc{1,1} = [1,3,4] I think, randi function uses uniform distribution. I want to control size of the set with some probabilty. For example I want my set to contain all 5 elements with a probability of .5 and and a set with 4, 3, 2,1 elements with probability .1 each. Is there a function that I could use in matlab ? Thanks

Answers (1)

Walter Roberson
Walter Roberson on 16 Dec 2013
y = randsample(n,k,true,w) or y = randsample(population,k,true,w) returns a weighted sample taken with replacement, using a vector of positive weights w, whose length is n. The probability that the integer i is selected for an entry of y is w(i)/sum(w). Usually, w is a vector of probabilities. randsample does not support weighted sampling without replacement.
  1 Comment
jana
jana on 16 Dec 2013
Walter,
I don't quite understand how that works in the problem specified above. I am particularly looking at choosing a set of some size with a specified probability.
For example : I would choose Sc[i,j] of size 4 with a probability .5 and so on. I am not sure how the above function is going to help me with that.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices 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!