Creating a matrix of random numbers which their distributions are stored in a table?

1 view (last 30 days)
i am trying to create a matrix of random numbers with each row following a certain probability distribution function (PDF) . also, PDFs are stored in another table such as this:
Code DistFit
____ _______________________
1057 'gamrnd(1.9948,5.231)'
1072 'wblrnd(1.1703,5.1103)'
1101 'wblrnd(1.0686,1547.6)'
1112 'wblrnd(2.0439,11.722)'
1114 'wblrnd(1.2258,14.977)'
for example i want to created a 5 * 30 matrix which its first row ( regarding the code "1057") follow a gamrnd(1.9948,5.231)
thank you beforehand
  3 Comments
Masoud Askari
Masoud Askari on 1 Dec 2019
Thank you for your answer Adam
the actual data is saved in an excel file and has 150 rows which i inputted into MATLAB using 'readtable'.
Adam Danz
Adam Danz on 1 Dec 2019
Edited: Adam Danz on 2 Dec 2019
In that case, my answer below should do the trick. Let me know if you have any questions.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 30 Nov 2019
It would be better to construct the DistFit table so that the function and the input values are in separate columns. That way you don't have to deconstruct the DistFit value into the function and input parts. The answer below assumes you're working with the table in your answer but if you are producing that table and are able to create it such that the function and inputs are in separates columns, a much cleaner solution is available.
% Re-create the first 3 rows of the table in the question
Code = [1057 1071 1101]';
DistFit = { 'gamrnd(1.9948,5.231)' ,'wblrnd(1.1703,5.1103)', 'wblrnd(1.0686,1547.6)'}.';
T = table(Code, DistFit);
% Define the size of the array and the code you want to look up
sz = [5,30];
code = 1101;
% decompose distFit into function and inputs
dist = regexprep(T.DistFit(T.Code==code),'\(.+\)','');
inputs = regexprep(T.DistFit(T.Code==code),{'.+(',')'},{'',''});
inputVals = cellfun(@str2double,strsplit(inputs{:},','),'UniformOutput',false);
fcn = str2func(dist{1});
% Compute the distribution with the given size
data = fcn(inputVals{:},sz);

Community Treasure Hunt

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

Start Hunting!