[Numpy-discussion] Any easy way to do this?
Neal Becker
ndbecker2@gmail....
Wed Mar 9 09:37:23 CST 2011
Angus McMorland wrote:
> 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.
Thanks! Now if I could understand why
w[:,:,None] - y[None,None] is what I needed...
More information about the NumPy-Discussion
mailing list