1

I have a numpy array that looks like this

X = numpy.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

I want to construct a view of the array, grouping its elements in a moving window (of size 4 in my example). My result should look like this :

M = numpyp.array([[1, 2, 3, 4],
              [2, 3, 4, 5],
              [3, 4, 5, 6],
              [4, 5, 6, 7],
              [5, 6, 7, 8],
              [6, 7, 8, 9]]

This is called a Hankel matrix, and I can use scipy.linalg.hankel to achieve that, or simply execute :

M=numpy.array([X[i:i+4] for i in range(len(X)-3)])

But I want to avoid the reallocation.

Is there a manner to have a view in the array X as the Hankel matrix described above without reallocation?

volatile
  • 899
  • 10
  • 22
  • @Divakar : is it an exact duplicate of the question in the link even if my problem is to avoid memory allocation? – volatile Jan 18 '17 at 17:57
  • Did you check out `Approach #2` there? It creates a view. Use `strided_app(X,4,1)` for your case. – Divakar Jan 18 '17 at 17:58

0 Answers0