MATLAB: 2d gaussian function

gaussiannested for

I need to plot a 2d gaussian function, where x and y corresponds to the image pixels, my code uses a nested for loop which makes my program run extremely slow, is there a way to write this in a more faster way?
(right now it takes about 8-10 sec to run on 1920*1080 size matrix and i need to produce 173,340 2d gaussian functions images which is too much time…)
thanks in advance for any help.
this is my current code:
function mat = gauss2d(mat, sigma, center)
gsize = size(mat);
for r=1:gsize(1)
for c=1:gsize(2)
mat(r,c) = gaussC(r,c, sigma, center);
end
end
function val = gaussC(x, y, sigma, center)
xc = center(1);
yc = center(2);
exponent = ((x-xc).^2 + (y-yc).^2)./(2*sigma);
val = (exp(-exponent));

Best Answer

function mat = gauss2d(mat, sigma, center)
gsize = size(mat);
[R,C] = ndgrid(1:gsize(1), 1:gsize(2));
mat = gaussC(R,C, sigma, center);