Jan. 22, 2007
5:23 p.m.
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)
7057
Age (days ago)
7057
Last active (days ago)
0 comments
1 participants
participants (1)
-
Keith Goodman