Assuming vector v
of size 1 x n
and function fun
that takes in a vector of length L
and returns a vector of size p x 1
.
Is there a MATLAB function that would take in vector v
, process each sliding window of length L
with function fun, and return a matrix of size p x n
(or p x (n-L)
).
I am aware this could be achieved with creating a matrix of windowed vectors with im2col
and processing each of those, but this takes too much memory for a long vector v
.
What I did here is define an anonymous function that, for a fixed
v
andl
and a starting index parameter (is
), gets the respective slice ofv
and appliesfun
to it.Then this function is applied, via
arrayfun
, to all useful values for this starting index. For reasons I myself cannot quite name at the moment, each application returns ap x 1
vector, butarrayfun
cannot arrange it into a proper matrix, thus the UniformOutput=false setting and thecell2mat
call around it.Edit: To test this with a function that turns 1-by-5 vectors into 4-by-1 vectors I used
and got this result:
Note again that in the definition of
funsl
v
is fixed, To apply this approach to differentv
you could make another function that takesv
(andl
, if you do not want to fix this) as parameter(s), contains the two lines above and returns the result of the second one.The same solution from this other post could apply (with some modifications):