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.

 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. 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. 6 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. 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 5 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. 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. 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.
##### MATLAB Release
MATLAB 6.1 (R12.1)