How to create a n to n matrix in python with input value diagonal
Question:
I am working on a probabilistic calculation and I could run my code for a small matrix like;
P_4 = np.array([
[0 ,1 ,0 , 0, 0],
[0 ,1/4,3/4, 0, 0],
[0 ,0 ,2/4,2/4, 0],
[0 ,0 ,0 ,3/4,1/4],
[0 ,0 ,0 , 0,1 ],
])
However, I would like to create a N*N matrix and to fill the values diagonally 0/n and next value 1 – 0/n.
n = 5
a = np.zeros((n,n),dtype = int)
np.fill_diagonal(a,np.array([range(1/n)]))
a
writing the above code gives me the error
TypeError: 'float' object cannot be interpreted as an integer
I would a appreciate any suggestions.
Answers:
I am working on a probabilistic calculation and I could run my code for a small matrix like;
P_4 = np.array([
[0 ,1 ,0 , 0, 0],
[0 ,1/4,3/4, 0, 0],
[0 ,0 ,2/4,2/4, 0],
[0 ,0 ,0 ,3/4,1/4],
[0 ,0 ,0 , 0,1 ],
])
However, I would like to create a N*N matrix and to fill the values diagonally 0/n and next value 1 – 0/n.
n = 5
a = np.zeros((n,n),dtype = int)
np.fill_diagonal(a,np.array([range(1/n)]))
a
writing the above code gives me the error
TypeError: 'float' object cannot be interpreted as an integer
I would a appreciate any suggestions.