Multivariate Gaussian user defined function

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

댓글 수: 1

the cyclist
the cyclist 2019년 11월 8일
Note that e is closer to 2.71, not 2.17.
You could also have used exp(1) there, for an even closer approximation.
Your code simply multiplied by e in your expression, rather than raising e to the desired power in the gaussian formula. That is another thing I fixed in my solution.

댓글을 달려면 로그인하십시오.

 채택된 답변

the cyclist
the cyclist 2019년 11월 8일
편집: the cyclist 2019년 11월 8일

0 개 추천

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)')

댓글 수: 3

BBB
BBB 2019년 11월 8일
편집: BBB 2019년 11월 8일
Thank you for your response. Somehow my code with the changes incorporated as suggested by you, is still not giving me the multivariate gaussian distribution.
But thank you very much for trying.
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);
BBB
BBB 2019년 11월 8일
Hi
Thank you so much! It works well :)
Really appreciate it!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

BBB
2019년 11월 8일

댓글:

BBB
2019년 11월 8일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by