I'm trying to fill the off diagonal elements on a 100x100 matrix, as shown below in the matlab code, so how to duplicate it in python.
T=(2*t0*diag(ones(1,100)))-(t0*diag(ones(1,99),1))-(t0*diag(ones(1,99),-1))
So I know that the first terms of RHS will fill the diagonal of the matrix with the value 2*t0
,
which I do in python as follows:
x = np.zeros((100,100))
np.fill_diagonal(x,2*t0)
but I don't know how to do the 2nd and the 3rd terms, I know that they will fill the values above and below the diagonal elements with value of -t0
, not all the off diagonal values but only filling the upper and lower value of diagonal with -t0, rest all are zeros, but I don't know how to write the python code for it.
I found this code:
# diagonal with offset from the main diagonal
diag([1,2,3], k=1)
will give output as:
array([[0, 1, 0, 0],
[0, 0, 2, 0],
[0, 0, 0, 3],
[0, 0, 0, 0]])
but how to apply it for a large matrix as in the case of my problem? I work in interactive python i.e. Anaconda, so which are the other packages that I can use for my problem?