Dietrich
Dietrich

Reputation: 5551

Building a matrix of 'rolled' rows efficiently in Numpy

I'd like to construct a (n,n)-array from a one dimensional array, where each row is shifted (with wrapping) by one relative to the previous one. The following code does this:

import numpy as np

r = np.array([1, 2, 3, 4, 5])
n = len(r)
MM = np.zeros((n, n), dtype=r.dtype)
for k in range(n):
    MM[k, :] = np.roll(r, k)

print(MM)

which results in:

[[1 2 3 4 5]
 [5 1 2 3 4]
 [4 5 1 2 3]
 [3 4 5 1 2]
 [2 3 4 5 1]]

Is there a way to do this Numpy faster, i.e., avoiding the for-loop, for large r in Numpy?

Upvotes: 3

Views: 194

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114976

Take a look at scipy.linalg.circulant

In [255]: r
Out[255]: array([1, 2, 3, 4, 5])

In [256]: circulant(r).T
Out[256]: 
array([[1, 2, 3, 4, 5],
       [5, 1, 2, 3, 4],
       [4, 5, 1, 2, 3],
       [3, 4, 5, 1, 2],
       [2, 3, 4, 5, 1]])

or scipy.linalg.toeplitz

In [257]: toeplitz(np.roll(r[::-1], 1), r)
Out[257]: 
array([[1, 2, 3, 4, 5],
       [5, 1, 2, 3, 4],
       [4, 5, 1, 2, 3],
       [3, 4, 5, 1, 2],
       [2, 3, 4, 5, 1]])

Upvotes: 4

Related Questions