Tuesday, April 21, 2009

Generating 2D, 3D, and nD Multinomials randomly

I have been much engrossed with the idea of Gaussian Mixture Models (GMMs) and their applicability to computer vision, especially in segmentation.

However, to be able to test a GMM, it is necessary to generate random vectors from Gaussian distributions. While it is true that Matlab has a function call mvnrnd() for this purpose, I have developed a function that produces a very similar result with great ease. You can click here for a detailed explanation of the code:

%% Function

% Generates normally distributed values

function p = normalMulti(mu, sigma, cases)

[row col] = size(mu);

if(col ~= 1)
    mu = mu';
    [row col] = size(mu);
end

[V D] = eig(sigma);

A = V*sqrt(D);

p = A*randn(row,cases) + repmat(mu,1,cases);