Problem 2356. Simulating the selection of a state with given probabilities

Solution 2119561

Submitted on 5 Feb 2020 by Wayne
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
probs = rand; while sum(probs) < 1 a = rand; if a + sum(probs) > 1 probs = [probs 1-sum(probs)]; break; else probs = [probs a]; end end states = 1:length(probs); for i = 1:100 y{i,1} = select_state(probs); [nelements,centers] = hist(y{i},states); probs_result{i} = nelements/length(y{i}); error(i,1) = sum(abs(probs-probs_result{i})); end assert(mean(error) <= 0.05 & mean(error) > 0);

Suggested Problems

More from this Author6

Community Treasure Hunt

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

Start Hunting!