Cody

Problem 42503. Generating random matrix with given probability mass function

Solution 2156699

Submitted on 11 Mar 2020 by HH
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 = 7.1941e-04