mask = np.tril(np.ones(3, dtype=bool)
print mask
[[ True False False]
[ True True False]
[ True True True]]
B = np.zeros(9)
B.shape = (3,3)
print B
[[ 0 0 0 ]
[ 0 0 0 ]
[ 0 0 0 ]]
B[mask]
array([0,0,0,0,0,0])
C = np.array([[1],[0],[0],[1],[0],[1]])
B[mask] = C
ValueError: boolean index array should have 1 dimension
I tried to apply .flatten():
B[mask] = C.flatten()
print B
array([[1, 0, 0],
[0, 0, 0],
[1, 0, 1]])
But my intended result is a diagonal matrix.
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
What am I doing wrong?
What am I doing wrong?- Assuming it's column major order. To solve it :B.T[mask.T] = C.flatten()I believe.