Smooths 2D array data. Ignores NaN's. function matrixOut = smooth2a(matrixIn,Nr,Nc) This function smooths the data in matrixIn using a mean filter over a rectangle of size (2*Nr+1)-by-(2*Nc+1). Basically, you end up replacing element "i" by the mean of the rectange centered on "i". Any NaN elements are ignored in the averaging. If element "i" is a NaN, then it will be preserved as NaN in the output. At the edges of the matrix, where you cannot build a full rectangle, as much of the rectangle that fits on your matrix is used (similar to the default on Matlab's builtin function "smooth"). "matrixIn": original matrix "Nr": number of points used to smooth rows "Nc": number of points to smooth columns. If not specified, Nc = Nr. "matrixOut": smoothed version of original matrix Written by Greg Reeves, March 2009. Division of Biology Caltech Inspired by "smooth2", written by Kelly Hilands, October 2004 Applied Research Laboratory Penn State University Developed from code written by Olof Liungman, 1997 Dept. of Oceanography, Earth Sciences Centre G�teborg University, Sweden E-mail: olof.liungman@oce.gu.se
0001 function matrixOut = smooth2a(matrixIn,Nr,Nc) 0002 % Smooths 2D array data. Ignores NaN's. 0003 % 0004 %function matrixOut = smooth2a(matrixIn,Nr,Nc) 0005 % 0006 % This function smooths the data in matrixIn using a mean filter over a 0007 % rectangle of size (2*Nr+1)-by-(2*Nc+1). Basically, you end up replacing 0008 % element "i" by the mean of the rectange centered on "i". Any NaN 0009 % elements are ignored in the averaging. If element "i" is a NaN, then it 0010 % will be preserved as NaN in the output. At the edges of the matrix, 0011 % where you cannot build a full rectangle, as much of the rectangle that 0012 % fits on your matrix is used (similar to the default on Matlab's builtin 0013 % function "smooth"). 0014 % 0015 % "matrixIn": original matrix 0016 % "Nr": number of points used to smooth rows 0017 % "Nc": number of points to smooth columns. If not specified, Nc = Nr. 0018 % 0019 % "matrixOut": smoothed version of original matrix 0020 % 0021 % 0022 % Written by Greg Reeves, March 2009. 0023 % Division of Biology 0024 % Caltech 0025 % 0026 % Inspired by "smooth2", written by Kelly Hilands, October 2004 0027 % Applied Research Laboratory 0028 % Penn State University 0029 % 0030 % Developed from code written by Olof Liungman, 1997 0031 % Dept. of Oceanography, Earth Sciences Centre 0032 % G�teborg University, Sweden 0033 % E-mail: olof.liungman@oce.gu.se 0034 0035 % 0036 % Initial error statements and definitions 0037 % 0038 if nargin < 2, error('Not enough input arguments!'), end 0039 0040 N(1) = Nr; 0041 if nargin < 3, N(2) = N(1); else N(2) = Nc; end 0042 0043 if length(N(1)) ~= 1, error('Nr must be a scalar!'), end 0044 if length(N(2)) ~= 1, error('Nc must be a scalar!'), end 0045 0046 % 0047 % Building matrices that will compute running sums. The left-matrix, eL, 0048 % smooths along the rows. The right-matrix, eR, smooths along the 0049 % columns. You end up replacing element "i" by the mean of a (2*Nr+1)-by- 0050 % (2*Nc+1) rectangle centered on element "i". 0051 % 0052 [row,col] = size(matrixIn); 0053 eL = spdiags(ones(row,2*N(1)+1),(-N(1):N(1)),row,row); 0054 eR = spdiags(ones(col,2*N(2)+1),(-N(2):N(2)),col,col); 0055 0056 % 0057 % Setting all "NaN" elements of "matrixIn" to zero so that these will not 0058 % affect the summation. (If this isn't done, any sum that includes a NaN 0059 % will also become NaN.) 0060 % 0061 A = isnan(matrixIn); 0062 matrixIn(A) = 0; 0063 0064 % 0065 % For each element, we have to count how many non-NaN elements went into 0066 % the sums. This is so we can divide by that number to get a mean. We use 0067 % the same matrices to do this (ie, "eL" and "eR"). 0068 % 0069 nrmlize = eL*(~A)*eR; 0070 nrmlize(A) = NaN; 0071 0072 % 0073 % Actually taking the mean. 0074 % 0075 matrixOut = eL*matrixIn*eR; 0076 matrixOut = matrixOut./nrmlize; 0077 0078 0079 0080 0081 0082 0083 0084 0085 0086 0087 0088