pythonnumpymatrixscipytoeplitz

How to generate a Toeplitz matrix in Python using a loop instead of built-in function


I'm trying to generate a simple Toeplitz matrix using for loop in Python

a = np.array([[3,4,6]])
b = np.zeros((np.size(a),np.size(a)))

b[0,:] = a
b[:,0] = a

for i in range(1,np.size(a)):
    for j in range(1,np.size(a)):
        a[i,j] = a[i-1,j-1]

should've worked, unless i'm missing something, but gives this error:

    a[i,j] = a[i-1,j-1]

IndexError: index 1 is out of bounds for axis 0 with size 1

How to make it Toeplitz without scipy's built-in toeplitz() function could you help?


Solution

  • I believe you meant

        b[i,j] = b[i-1,j-1]
    

    Not a, which clearly has only one dimension.