[Numpy-discussion] efficient function calculation between two matrices
Robert Kern
robert.kern@gmail....
Tue Feb 17 10:36:40 CST 2009
On Tue, Feb 17, 2009 at 04:09, John [H2O] <washakie@gmail.com> wrote:
>
> hello,
>
> I am trying to calculate the results of a function between two matrices:
>
>>>> F.shape
> (170, 2)
>>>> T.shape
> (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
> (170,481)
>
> containing the results of my distance function:
>
> gcd(x1,y1,x2,y3):
> """ 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[0],f[1],t[0],t[1]]) 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])
--
Robert Kern
"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
mailing list