I don't have the package for nlfilter
and I didn't quite follow this example.
I have a really simple function fun
and I want to apply it to a moving window of an array. The array is Nx1
, and I want to look at length k
intervals, say. So for N=10
and k=3
and fun = @(x) min(x);
I would get
A = [13 14 2 14 10 3 5 9 15 8];
filter(A,k,fun) = [2 2 2 3 3 3 5 8];
Here I only want to look at indices 1,2,3 then 2,3,4 then ... then 8,9,10, so the final sequence is length 7. I can do this easy with a for loop, but I have no idea how to vectorize it for Matlab. Help, please. Thanks.
The post you mentioned gave a general solution for building sliding windows (you could control: overlapping vs. distinct, slide step, overlap amount, windows size)
In your case, it is much simpler and can be easily performed with the HANKEL function:
If you want to build a reusable solution:
which we use as:
Note I am using CELLFUN in case
fcn
cannot operate across dimensions in a vectorized manner...Here is one very simple and fast way to do it:
EDIT: Since you want a full function...