0

I am trying to make a special diagonal matrix that looks like this:

[[1,1,0,0,0,0],
 [0,0,1,1,0,0],
 [0,0,0,0,1,1]]

It is slightly different from the question here: Make special diagonal matrix in Numpy

I tried tweaking the solution but couldn't quite get it. Appreciate any advice on how to achieve this efficiently.

yl_low
  • 1,209
  • 2
  • 17
  • 26

1 Answers1

0

Not as elegant as in comments, but :

a=4 # number of rows
b=a*2 #number of columns
np.array((([1]*2+[0]*b)*a)[:-b]).reshape(a,b)
array([[1, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 1]])

works for any a.

Artem Trunov
  • 1,340
  • 9
  • 16