0

I'm trying to use the meshgrid "function" in Matlab to generate the value of the characteristic function phi of a bivariate normal distribution. Currently I am using two-for loops to do this.

for i = 1:M
   for j = 1:M
   U = [u1(i); u2(j)];
   phi(i,j) = exp(-1i*mu*U-0.5*U'*SIGMA*U); 
   end
end

Here u1 and u2 are the two vectors spanning the space for which values phi can take. However, this double for-loop is very time consuming, and I would like to do this using a meshgrid approach.

U = meshgrid(u1,u2);
PHI = exp(-1i*mu*[U1(:) U2(:)]-0.5*[U1(:) U2(:)]'*SIGMA.*[U1(:) U2(:)]);

I am trying to do this using the code above. But this is definitely not working due to the different dimensions of mu and [U1(:) U2(:)]. Do anyone have any hints on how this can be done?

vgdev
  • 57
  • 7
  • For me the line `phi(i,j) = exp(-1i*mu*U-0.5*U'*SIGMA*U);` doesn't even work, as `-1i*mu*U` is a 2-by-1 vector and the left hand side is a scalar. Could you clarify? Maybe add missing values `mu` and `SIGMA` to your question. Also the variables `U1` and `U2` are not defined. – knedlsepp Dec 13 '14 at 14:52

2 Answers2

1

This should do the trick:

[U1, U2] = meshgrid(u1,u2);
U = [U1(:) U2(:)]';
PHI = reshape(exp(-1i*mu*U-0.5*sum(U.*(SIGMA*U),1)),M,M).';

PS: Using ndgrid seems to be preferable to meshgrid also in this situation, because we don't need to transpose afterwards.

[U1, U2] = ndgrid(u1,u2);
U = [U1(:) U2(:)]';
PHI = reshape(exp(-1i*mu*U-0.5*sum(U.*(SIGMA*U),1)),M,M);
Community
  • 1
  • 1
knedlsepp
  • 6,065
  • 3
  • 20
  • 41
0

Solved it by just writing out the matrix multiplications:

    [U1,U2]= meshgrid(u1,u2);
PHI = exp(-1i.*(mu_adj(1)*U2+mu_adj(2)*U1)...
    -0.5*(SIGMA(1,1)*U2.^2+(SIGMA(2,1)+SIGMA(1,2))*U1.*U2+SIGMA(2,2)*U1.^2));
vgdev
  • 57
  • 7