Cody

Problem 42503. Generating random matrix with given probability mass function

Solution 2846199

Submitted on 17 Aug 2020 by Dennis Gimlin
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
rnd = sort(rand(randi([10,20]),1)); prob = vertcat(rnd(1,:),diff(rnd,1,1),1-rnd(end,:)); sz = [1 1e5;1e5 1;1e3 1e2;randi([100 200], 100, 2)]; sz = sz(randi(size(sz,1)),:); x = rndsampling(sz(1),sz(2),prob); prob_est = histcounts(x,1:numel(prob)+1,'Normalization','probability').'; err = mean(abs(prob_est - prob)) assert(err < 0.005 && isequal(size(x),sz) && all(~isnan(x(:))));

err = 0.0015

Suggested Problems

More from this Author30

Community Treasure Hunt

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

Start Hunting!