[Numpy-discussion] Any easy way to do this?

Angus McMorland amcmorl@gmail....
Wed Mar 9 08:56:05 CST 2011


On 9 March 2011 09:45, Neal Becker <ndbecker2@gmail.com> wrote:
> given: w[i,j,k], y[l, k]
>
> find:
> d[l,i,j] = norm(w[i,j] - y[l])
>
> for each triple (l,i,j), w[i,j]-y[l] is a vector, of which I want to find the
> norm, and store into d[l,i,j]

Is something like this what you want to do?

w = np.random.randint(100, size=(4,5,6))
y = np.random.randint(100, size=(7,6))
norm = lambda x, axis: np.sqrt(np.sum(x**2, axis=axis))
d = norm(w[:,:,None] - y[None,None], -1)

Angus.
-- 
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh


More information about the NumPy-Discussion mailing list