New to MatLab: How to enter given data for a random variable (&find /fit distribution function)

3 views (last 30 days)
Hello,
I am very new to MatLab so I am still struggling a lot.
What I have given:
I have a random variable X with thoutcomes and probabilites.
How do I put this data in MatLab? I know this is very basic but I am struggling with it. In the next step I need to find and draw a fitting distribution function, I think I know how to do this as I found some tutorials, but all these tutorials only used sample data sets and not data sets they had to put in theirselves.
Thanks already!

Answers (3)

the cyclist
the cyclist on 8 May 2022
If you have the Statistics and Machine Learning Toolbox, you can use the randsample function:
N = 100;
outcome = [1; 3; 4; 5; 9; 10];
prob = [0.20; 0.15; 0.10; 0.05; 0.35; 0.15];
y = randsample(outcome,1000000,true,prob);
will give 100 samples from those outcomes, weighted by the probabilities.
  1 Comment
Lisa Maria Teppich
Lisa Maria Teppich on 8 May 2022
Thanks for your answer! I dont have this/ am not sure whether I am allowed to use if for the assignment. Is there another way to do so without it?

Sign in to comment.


Torsten
Torsten on 8 May 2022
Edited: Torsten on 8 May 2022
n = 30;
sample = random(n)
function sample = random(n)
uniform = rand(n,1);
sample = zeros(size(uniform));
for i = 1:n
v = uniform(i);
if v <= 0.2
s = 1;
elseif v > 0.2 && v <= 0.35
s = 3;
elseif v > 0.35 && v <= 0.45
s = 4;
elseif v > 0.45 && v <= 0.5
s = 5;
elseif v > 0.5 && v <= 0.85
s = 9;
else
s = 10;
end
sample(i) = s;
end
end

the cyclist
the cyclist on 8 May 2022
Edited: the cyclist on 8 May 2022
Here is a method that does not require the Statistics and Machine Learning Toolbox:
% Input data
N = 100;
outcome = [1; 3; 4; 5; 9; 10];
prob = [0.20; 0.15; 0.10; 0.05; 0.35; 0.15];
% Algorithm
r = rand(1,N);
index = numel(outcome) - sum(r <= cumsum(prob)) + 1;
X = outcome(index);

Community Treasure Hunt

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

Start Hunting!