smooth2
Smooths the data in a 2D matrix with a user-defined resolution.
Author: Kelly Hilands

I took out the for loops and just used array algebra to do essentially the same thing. Improved speed ~10 fold. I didn't bother checking whether edge behavior is the same. My edge behavior is similar to the default behavior in Matlab's builtin smooth function.

%Initial error statements and definitions
if nargin<2, error('Not enough input arguments!'), end

N(1) = Nr;
if nargin<3
N(2) = N(1);
else
N(2) = Nc;
end

if length(N(1))~=1, error('Nr must be a scalar!'), end
if length(N(2))~=1, error('Nc must be a scalar!'), end

[row,col] = size(matrixIn);
eL = spdiags(ones(row,2*N(1)+1),(-N(1):N(1)),row,row);
eL = eL./(repmat(sum(eL,1),row,1));
eR = spdiags(ones(col,2*N(2)+1),(-N(2):N(2)),col,col);
eR = eR./(repmat(sum(eR,2),1,col));

The function works completely as advertised. Thank you.

In response to JEC PsiCom's comment from 2011 (since I stumbled across the same error)... Make sure your input matrix is of type "double". My problem was solved by casting the input as a double {eg, xs = smooth2a(double(x),1);}

18 Oct 2013

smooth2a
Smooths a 2D matrix using a mean filter over a user-defined rectangle. Ignores and preserves NaNs.
Author: Greg Reeves

The function works completely as advertised. Thank you.
In response to JEC PsiCom's comment from 2011 (since I stumbled across the same error)... Make sure your input matrix is of type "double". My problem was solved by casting the input as a double {eg, xs = smooth2a(double(x),1);}

Comment only

18 Oct 2013

smooth2a
Smooths a 2D matrix using a mean filter over a user-defined rectangle. Ignores and preserves NaNs.

I get this error trying to run the code on a 2-D matrix of type double:
"MTIMES is not supported for one sparse input and one single input."
What does this mean?

Comment only

02 Jan 2011

smooth2a
Smooths a 2D matrix using a mean filter over a user-defined rectangle. Ignores and preserves NaNs.

Comment only