I am testing a function containing nlfilter
on a matrix. For the purpose I have created a random 11X11 matrix and use a 7x7 moving window with the help of nlfilter
. My function is as follows:
function funct(fh)
I = rand(11,11)
ld = input('Enter the lag = ') % prompt for lag distance
fh = @dirvar,@diagvar;
A = nlfilter(I, [7 7], fh);
% Subfunction
function [h] = dirvar(I)
c = (size(I)+1)/2
EW = I(c(1),c(2):end)
h = length(EW) - ld
end
% Subfunction
function [h] = diagvar(I)
c = (size(I)+1)/2
NE = diag(I(c(1):-1:1,c(2):end))
h = length(NE) - ld
end
end
When I run funct('dirvar')
it asks for lag, selects 4 elements of first row and progresses element by element. From 9th to 11th element of the first row it takes 0s as last elements(automatic padding) which is expected behavior.
But when I run funct('diagvar')
the function behaves the same(as in dirvar) instead of selecting elements diagonally and going for padding. For 1st row I expect it to select first element from first row and 3 zeros and so on till end of row; when it comes to 2nd row - 1st element it will be the 2nd row - 1st element + 1st row - 2nd element followed by 2 zeros and so on.
If I just create a random matrix of order 11X11 and run lines from diagvar
it selects the central value from the matrix and progresses as expected.