rusty009
rusty009

Reputation: 876

Normal distribution function in matlab

apologies for the silly question, mathematics is not my strong point. I am trying to design a function in matlab that generates samples according to a normal distribution N(mu, sigma) in d-dimensions. This is the code I have so far,

mu = [1 2];
Sigma = [1 .5; .5 2]; R = chol(Sigma);
z = repmat(mu,100,1) + randn(100,2)*R;

I've found this from reading through various wikipedia and google articles and was wondering if it is correct? Thanks in advance,

rusty

Upvotes: 1

Views: 1539

Answers (1)

user85109
user85109

Reputation:

Short answer - yes.

Slightly longer answer - why not try it yourself?

>> z = repmat(mu,1000000,1) + randn(1000000,2)*R;
>> mean(z)
ans =
        1.001       2.0005
>> cov(z)
ans =
      0.99937      0.49942
      0.49942       2.0017

Upvotes: 2

Related Questions