python - How to make a diagonal matrix with canopy that has a displacement in the diagonal? -
i need create diagonal matrix diagonal displaced left or right center help?
as down-votes of question suggest, should try explain question in better way. being said, best guess you're looking superdiagonal , subdiagonal matrices; i.e.:
superdiagonal:
0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 subdiagonal:
0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 if that's case, can use numpy's indices:
import numpy np superdiagonal = np.zeros((4, 4)) i, j = np.indices(superdiagonal.shape) superdiagonal[i == j-1] = 1 print(superdiagonal) this give you:
array([[ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.], [ 0., 0., 0., 0.]]) for subdiagonal, have change i == j-1 part i == j+1.
Comments
Post a Comment