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?

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

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.