File Exchange

## Multivariate Gaussian Distribution

version 1.0 (2.05 KB) by

Calculates samples from a multivariate Gaussian distribution.

3.94737
19 Ratings

Updated

Creates a number of samples from a specified number of dimensions and centers them around a given mean, and within a given covariance range. You might not find it very useful, but hey, I need something to do this so why not.

To use:
You need to generate 1000 samples from a 3 dimensional Gaussian distribution with a mean m = [4,5,6], and with a covariance sigma = [9 0 0;0 9 0;0 0 9].

Command line:
x=mgd(1000,3,m,sigma) or x=mgd(1000,3,m',sigma)
it doesn't matter if the mean is given as a row or column vector

x is a (1000x3) matrix of the where
where each row is the coordinates of that point in 3 space.

Krishna Kumar Kottakki

### Krishna Kumar Kottakki (view profile)

excellent.....thanks allot.

Xiu

### Xiu (view profile)

is mgd a built-in matlab function?

Ahmed Fasih

### Ahmed Fasih (view profile)

Use "x=bsxfun(@plus, x, rmean)" instead of repmat.

M Taj

DO NOT WASTE YOUR TIME. The code looks wrong

A waste of time trying to use this file. Better write it yourself. It generates totally wrong distributions. DO NOT WASTE YOUR TIME.

hendri hendri

Tim Felty

Dan McClanahan: Actually upon further testing in the situation you describe the error seems to be cause only by the failure to account for the calculated mean of one sample in the code being a scalar.

Tim Felty

Dan McClanahan: The problem with mean stems from the fact that the code subtracts off the current sample mean assuming that its going to be two dimensional as well.

The covariance issue seems to be cause by the single sample getting multiplied by the covariance matrix. As to why this is causing problems I'm not to sure. But since that is the case it might be better to generate your samples directly from the distribution.

Dan McClanahan

Code works well, except....
Generating 2D gaussian works great when I create a vector of samples. However, let's say I want to generate N samples but rather than generating all N at once I make N successive calls to the mgd function. Here, the function breaks down.

Matteo Zandi

Works even with d=2, thanks

Sherman Kuo

Excellent Work !!!

ahmed ased

hicham laanaya

thanks a lot!

Timma Gupta

thank you..!

Daniel Mark

Thank you so much for your code!

Daniel

Seblegit SIrgaga

Please try to write the program with out a for loop. Matlab is a good software for matrix manupulation. FOr loops make programs slower. Just use,
x=x*sqrt(variance)+ones(N,1)*mean(x)

Gee Didilly

pa pa ooo mow mow

yc wang

in 1-D normal distribution, when sigma=0, it doesn't work.

very good but I couldn't understand it's algorithm

lei fang

very good, very convinient to use. helpful to finish course project

Bob Sturm

Works good, except when I want only a few realizations. Freezes when I ask for 1x1 result.