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

3 次查看(过去 30 天)
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!

回答(3 个)

the cyclist
the cyclist 2022-5-8
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.

Torsten
Torsten 2022-5-8
编辑:Torsten 2022-5-8
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 2022-5-8
编辑:the cyclist 2022-5-8
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!

Translated by