Skip to content
Advertisement

Vectorized way to construct a block Hankel matrix in numpy (or scipy)

I want to contrsuct the following matrix :

JavaScript

where each v(k) is a (ndarray) vector, say from a matrix

JavaScript

Using a for loop, I can do something like this for example:

JavaScript

And I get :

JavaScript

Is there any way to construct this matrix in a vectorized way (which I imagine would be faster than for loops when it comes to large matrices ?).

Thank you.

Advertisement

Answer

This looks about optimal; you did a good job vectorizing it already. The only improvement I can make is to replace np.zeros with np.empty, which skips initializing the array. I tried using np.vstack and np.lib.stride_tricks.sliding_window_view (after https://stackoverflow.com/a/60581287) and got the same performance as the for loop with np.empty.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement