I'm not sure and array of tuple is what you want in the meshgrid case,
I mean can you use it to compute say cos(X*Y) (which is usually what you 
want to use meshgrid for) ?

It's also linked to the problem of evaluating a Gaussian, the solution 
given here use meshgrid:

X, Y = meshgrid(linspace(-1,1,100),linspace(-1,1,100))D = sqrtm(X*X + 
Y*Y)sigma, mu = 1.0, 0.0G = exp(-( (D.-mu)^2 / ( 2.0 * sigma^2 ) ) )


You can do it with array comprehension:

sigma, mu = 1.0, 0.0

G = [ exp(-(x-mu).^2/(2.0*sigma^2) -(y-mu).^2/(2.0*sigma^2) ) for x in 
linspace(-1,1,100), y in linspace(-1,1,100) ]

It look like a more elegant solution in this case, but this kind of 
one-liner can also become unreadable. Maybe a loop is still the most 
generic solution:

sigma, mu = 1.0, 0.0
x,y = linspace(-1,1,100), linspace(-1,1,100)
G = zeros(length(x),length(y))

for i in 1:length(x), j in 1:length(y)
    G[i,j] = exp(-(x[i]-mu).^2/(2.0*sigma^2) -(y[j]-mu).^2/(2.0*sigma^2) )
end

Reply via email to