Make special diagonal matrix in Numpy

前端 未结 5 1330
时光取名叫无心
时光取名叫无心 2020-12-06 01:09

I am trying to make a numpy array that looks like this:

[a b c       ]
[  a b c     ]
[    a b c   ]
[      a b c ] 

So this involves updat

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 02:08

    I can't comment yet, but I want to bump that ali_m's answer is by far the most efficient as scipy takes care of things for you.

    For example, with a matrix of size n,m = 1200, repeatedly adding np.diag() calls takes ~6.14s, Saullo G. P. Castro's answer takes ~7.7s, and scipy.linalg.toeplitz(np.arange(N), np.arange(N)) takes 1.57ms.

提交回复
热议问题