5
$\begingroup$

Here is my implementation of the built-in GaussianMatrix function.

myGaussianMatrix[{x_, y_}, sigma]:= Exp[-{x^2 + y2}/(2 sigma^2)]

(*generate a grid with r = 2 means sigma = 1*) 
grid = grid = Flatten[Table[{x, y}, {x, -r, r}, {y, -r, r}], 1]
kernel = myGaussianMatrix[#, 1] & /@ grid
(*Normalize*)
ArrayReshape[(kernel/Total[kernel]), {2r+1, 2r+1}]

(*output*)
{{0.00296902, 0.0133062, 0.0219382, 0.0133062, 
0.00296902}, {0.0133062, 0.0596343, 0.0983203, 0.0596343, 
0.0133062}, {0.0219382, 0.0983203, 0.162103, 0.0983203, 
0.0219382}, {0.0133062, 0.0596343, 0.0983203, 0.0596343, 
0.0133062}, {0.00296902, 0.0133062, 0.0219382, 0.0133062, 
0.00296902}} 

Built-in

GaussianMatrix[2]
(*Outputs*)

{{0.002589, 0.0107788, 0.0241466, 0.0107788, 0.002589}, {0.0107788, 
  0.0448755, 0.10053, 0.0448755, 0.0107788}, {0.0241466, 0.10053, 
  0.225206, 0.10053, 0.0241466}, {0.0107788, 0.0448755, 0.10053, 
  0.0448755, 0.0107788}, {0.002589, 0.0107788, 0.0241466, 0.0107788, 
  0.002589}}

What is the issue? Is it the normalization? or Is it a bug?

$\endgroup$

1 Answer 1

8
$\begingroup$

With the option setting Method->"Gaussian", GaussianMatrix gives the same result as your approach:

GaussianMatrix[2, Method -> "Gaussian"]

{{0.00296902, 0.0133062, 0.0219382, 0.0133062, 0.00296902}, {0.0133062, 0.0596343, 0.0983203, 0.0596343, 0.0133062}, {0.0219382, 0.0983203, 0.162103, 0.0983203, 0.0219382}, {0.0133062, 0.0596343, 0.0983203, 0.0596343, 0.0133062}, {0.00296902, 0.0133062, 0.0219382, 0.0133062, 0.00296902}}

The default setting is "Bessel":

GaussianMatrix[2] == GaussianMatrix[2, Method -> "Bessel"]

True

Docs >> GaussianMatrix >> Scope:

Mathematica graphics Mathematica graphics

$\endgroup$
1
  • 1
    $\begingroup$ In addition, setting WorkingPrecision -> Infinity will show the explicit expressions used for the entries. $\endgroup$ Apr 3, 2016 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.