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.
  댓글 수: 1
Lisa Maria Teppich
Lisa Maria Teppich 2022년 5월 8일
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?

댓글을 달려면 로그인하십시오.


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