python - Converting 1D numpy array into top triangle of matrix -


is possible take 1d numpy array containing 171 values , place these values top triangle of 18x18 matrix?

i feel should able use np.triu_indices somehow can't quite figure out!

thanks!

see what efficient way kind of matrix 1d numpy array? , copy flat list of upper triangle entries full matrix?

roughly approach is

result = np.zeros(...) ind = np.triu_indices(...) result[ind] = values 

details depend on size of target array, , layout of values in target.

in [680]: ind = np.triu_indices(4) in [681]: values = np.arange(16).reshape(4,4)[ind] in [682]: result = np.zeros((4,4),int) in [683]: result[ind]=values in [684]: values out[684]: array([ 0,  1,  2,  3,  5,  6,  7, 10, 11, 15]) in [685]: result out[685]:  array([[ 0,  1,  2,  3],        [ 0,  5,  6,  7],        [ 0,  0, 10, 11],        [ 0,  0,  0, 15]]) 

Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -