New to MatLab: How to enter given data for a random variable (&find /fit distribution function)
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!
0 comentarios
Respuestas (3)
the cyclist
el 8 de Mayo de 2022
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
el 8 de Mayo de 2022
Editada: Torsten
el 8 de Mayo de 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
0 comentarios
the cyclist
el 8 de Mayo de 2022
Editada: the cyclist
el 8 de Mayo de 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);
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!