[Numpy-discussion] array is not broadcastable to correct shape
Keith Goodman
kwgoodman at gmail.com
Mon Jan 22 12:23:46 EST 2007
What is the simplest way to make the sum of the positive elements in a
Nx1 matrix equal 1?
I end up converting the matrix to an array, doing the normalization,
then converting the array back to a matrix.
>> m = M.randn(10,1)
>> m[m > 0,:] = m[m > 0,:] / m[m > 0,:].sum()
---------------------------------------------------------------------------
exceptions.ValueError Traceback (most
recent call last)
<ipython console>
ValueError: array is not broadcastable to correct shape
>>
>> a = M.asarray(m)
>> a[a > 0,:] = a[a > 0,:] / a[a > 0,:].sum()
>> a[a > 0,:].sum()
1.0
>> m = M.asmatrix(a)
More information about the NumPy-Discussion
mailing list