funsl=@(is) fun(v(is:is+l-1));
cell2mat(arrayfun(funsl,1:length(v)-l+1,'UniformOutput',false))
What I did here is define an anonymous function that, for a fixed v
and l
and a starting index parameter (is
), gets the respective slice of v
and applies fun
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 a p x 1
vector, but arrayfun
cannot arrange it into a proper matrix, thus the UniformOutput=false setting and the cell2mat
call around it.
Edit: To test this with a function that turns 1-by-5 vectors into 4-by-1 vectors I used
l=5;v=1:12; fun=@(x) cumsum(x(2:end))';
and got this result:
ans =
2 3 4 5 6 7 8 9
5 7 9 11 13 15 17 19
9 12 15 18 21 24 27 30
14 18 22 26 30 34 38 42
Note again that in the definition of funsl
v
is fixed, To apply this approach to different v
you could make another function that takes v
(and l
, if you do not want to fix this) as parameter(s), contains the two lines above and returns the result of the second one.