Code covered by the BSD License

### Highlights from Multivariate Gaussian Distribution

3.94737
3.9 | 19 ratings Rate this file 20 Downloads (last 30 days) File Size: 2.05 KB File ID: #5984 Version: 1.0

# Multivariate Gaussian Distribution

### Timothy Felty (view profile)

05 Oct 2004 (Updated )

Calculates samples from a multivariate Gaussian distribution.

File Information
Description

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.

Acknowledgements

This file inspired Gaussian Random Samples Generation.

MATLAB release MATLAB 6.1 (R12.1)
Other requirements As far as I know it will work as long as you have randn function. Send me an email if it doesn't.
14 Oct 2011 Krishna Kumar Kottakki

### Krishna Kumar Kottakki (view profile)

excellent.....thanks allot.

20 Sep 2011 Xiu

### Xiu (view profile)

is mgd a built-in matlab function?

Comment only
15 Aug 2011 Ahmed Fasih

### Ahmed Fasih (view profile)

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

22 Nov 2007 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.

26 Sep 2006 hendri hendri
14 Sep 2006 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.

Comment only
14 Sep 2006 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.

Comment only
13 Sep 2006 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.

09 Sep 2006 Matteo Zandi

Works even with d=2, thanks

24 Apr 2006 Sherman Kuo

Excellent Work !!!

28 Mar 2006 ahmed ased
15 Mar 2006 hicham laanaya

thanks a lot!

09 Feb 2006 Timma Gupta

thank you..!

31 Jan 2006 Daniel Mark

Thank you so much for your code!

Daniel

09 Dec 2005 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)

08 Dec 2005 Gee Didilly

pa pa ooo mow mow

28 Nov 2005 yc wang

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

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

02 May 2005 lei fang

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

01 Feb 2005 Bob Sturm

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

11 Oct 2004

I have updated, I made some small changes to save memory that I was using carelessly, and fixed some spelling. Stuff like that. Still works the same way.

13 Oct 2004

I have updated, I made some small changes to save memory that I was using carelessly, and fixed some spelling. Stuff like that. Still works the same way.

05 Dec 2005

The older version had problems when creating distributions in a large number of dimensions. I rewrote the code to use a different method. The whitening transform used earlier was causing errors. This should be a drop in replacement.

12 Dec 2005

There was a review that says I should get rid of the for loops. So I did the job with repmat. Should just drop in to any existing uses of mgd.m

06 Feb 2006

A user emailed me about a better way of getting the covariance. Thanks Alan. The new method requires the variance matrix to be positive-definite, however if the variance is not, it will use the old method.

21 Mar 2006

Repost: Something went wrong last month
A user emailed me about a better way of getting the covariance. Thanks Alan. The new method requires the variance matrix to be positive-definite, however if the variance is not, it will use the old method.

14 Sep 2006

Due to notes from Matteo and Dan, I edited the code so that it doesn't subtract of the mean initial sample mean. The returned mean will be off as much as randn is for the sample size you are using. Fixes bug brought up by Dan. Thanks Dan and Matteo.