[Numpy-discussion] efficient function calculation between two matrices
Tue Feb 17 10:36:40 CST 2009
On Tue, Feb 17, 2009 at 04:09, John [H2O] <email@example.com> wrote:
> I am trying to calculate the results of a function between two matrices:
> (170, 2)
> (170, 481, 2)
> Where F contains lat/lon pairs and T contains 481 lat/lon pairs for 170
> trajectories of length 481
> I want a new array of shape
> containing the results of my distance function:
> """ returns great circle distance"""
> return dist
> Is there a way to do this without the use of loops?
> I'm thinking something along the lines of:
> distances = [[gcd(f,f,t,t]) for f in F] for t in T]
> But obviously this doesn't work...
Change the shape of T to (481, 170, 2). Then it will be
broadcast-compatible with the (170, 2) array as long as your gcd()
function is written to take advantage of broadcasting.
distances = gcd(F[:,0], F[:,1], T[:,:,0], T[:,:,1])
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion