Sounds like you want something like:
@views buffer(X, n, p=0) = [X[i:i+n-1] for i in firstindex(X):n-p:lastindex(X)-n+1]
This returns an array of views of X
rather than copies.
If you want to stack these “frames” into a matrix, similar to Matlab, you can do stack(buffer(X, n, p))
, but I would eventually try to re-think your code to avoid making copies of all the data like this. (In general, Matlab contorts you into a particular “vectorized” style of programming because Matlab loops are slow, and there is often a faster and more natural way to do things in Julia once you get used to it.)
e.g.
julia> stack(buffer(1:11, 3, 1))
3×5 Matrix{Int64}:
1 3 5 7 9
2 4 6 8 10
3 5 7 9 11
I’m not sure if this is exactly the desired output? I don’t have Matlab handy, but the GNU Octave buffer
function puts zeros at the beginning and end for some reason:
octave:1> buffer(1:11, 3, 1)
ans =
0 2 4 6 8 10
1 3 5 7 9 11
2 4 6 8 10 0
You could easily tweak my implementation above to do this, e.g. by first zero-padding X
(or, to avoid copies, by wrapping X
in a zero-PaddedView
from PaddedViews.jl). (The explicit-loop implementation by @GunnarFarneback in another thread has the zero-padding built-in, but doesn’t return views.)