How can I fill the elements of the lower triangular part of a matrix, including the diagonal, with values from a column vector?
For example i have :
m=np.zeros((3,3))
n=np.array([[1],[1],[1],[1],[1],[1]]) #column vector
I want to replace values which have indices of (0,0),(1,0),(1,1),(2,0),(2,1),(2,2)
from m
with the vector n
, so I get:
m=np.array([[1,0,0],[1,1,0],[1,1,1]])
Then I want make the same operation to m.T
to get as a result:
m=np.array([[1,1,1],[1,1,1],[1,1,1]])
Can someone help me please? n
should be a vector with shape(6,1)
I'm not sure if there's going to be a clever numpy-specific way of doing this, but it looks relatively straightforward like this:
import numpy as np
m=np.zeros((3,3))
n=np.array([[1],[1],[1],[1],[1],[1]]) #column vector
indices=[(0,0),(1,0),(1,1),(2,0),(2,1),(2,2)]
for ix, index in enumerate(indices):
m[index] = n[ix][0]
print(m)
for ix, index in enumerate(indices):
m.T[index] = n[ix][0]
print(m)
Output of the above is:
[[1. 0. 0.]
[1. 1. 0.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]