Issue
given matrix:
x = matrix([[ 0.9, 0.14], [ 0.15, 0.8]])
how can you make the first column, x[:,0]
, into a diagonal matrix in numpy? to get:
matrix([[0.9, 0],
[0, 0.15]])
Solution
numpy.diag( x.A[ :, 0 ] )
should do it.
The difference between a matrix
and an array
is crucial here. You won't get the same result from just numpy.diag( x[ :, 0 ] )
. x.A
is a shorthand for numpy.asarray( x )
when x
is a matrix
.
So by the same token, to answer your question precisely I guess I shouldn't forget convert the answer from an array
back to a matrix
:
numpy.matrix( numpy.diag( x.A[ :, 0 ] ) )
Answered By - jez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.