- Define L as the first column of X, not just first value
- Ditto Y for second column
- Changed a couple matrix operations to elementwise operations, in the definition of z

4 views (last 30 days)

Hi

I want to create a hard coded multivariate gaussian function. I know there is an already existing matab function but I need to create another function for my project.

The Multivariate Gaussian Distribution equation given is as follows:

The function has to give a final plot of the gaussian bump using the imagesc in 2D.

%% Some code that I have already tried

function [z] = mygaussian2Dplot(X,mu,sigma)

e = 2.17;

L = X(1);

Y = X(2);

sigma_l = sigma(1).*L;

sigma_y = sigma(2).*Y;

z = 1/2.*sigma_l.*sigma_y.*pi*e.*(-L.^2/2.*sigma_l.^2 - Y.^2/2.*sigma_y.^2);

figure

imagesc(z)

title('Multivariate Gaussian Distribution')

xlabel('x')

ylabel('f(x)')

end

the cyclist
on 8 Nov 2019

Edited: the cyclist
on 8 Nov 2019

I made a few changes:

- Define L as the first column of X, not just first value
- Ditto Y for second column
- Changed a couple matrix operations to elementwise operations, in the definition of z

L = X(:,1);

Y = X(:,2);

sigma_l = sigma(1).*L;

sigma_y = sigma(2).*Y;

z = 1/2.*sigma_l.*sigma_y.*pi.*exp(-L.^2./2.*sigma_l.^2 - Y.^2./2.*sigma_y.^2);

figure

imagesc(z)

title('Multivariate Gaussian Distribution')

xlabel('x')

ylabel('f(x)')

the cyclist
on 8 Nov 2019

L = X(:,1);

Y = X(:,2);

sigma_l = sigma(1).*L;

sigma_y = sigma(2).*Y;

[LL,YY] = ndgrid(L,Y);

z = 1/2.*sigma_l.*sigma_y.*pi.*exp(-LL.^2./2.*sigma_l.^2 - YY.^2./2.*sigma_y.^2);

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/489977-multivariate-gaussian-user-defined-function#comment_765270

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/489977-multivariate-gaussian-user-defined-function#comment_765270

Sign in to comment.