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

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -