# nanmoving_average2

## PURPOSE

NANMOVING_AVERAGE2 Smooths a matrix through the moving average method.

## SYNOPSIS

function [X,A] = nanmoving_average2(X,Fr,Fc,INT)

## DESCRIPTION

    NANMOVING_AVERAGE2   Smooths a matrix through the moving average method.

       Syntax:
         [Y,Nsum] = nanmoving_average2(X,Fr,Fc,INT);

       Input:
         X   - Matrix of finite elements.
         Fr  - Window semi-length in the rows. A positive scalar (default 0).
         Fc  - Window semi-length in the columns. A positive scalar (default
               Fr).
         INT - If INT=0: do not interpolates NaN's (default); elseif INT=1 do
               interpolates.

       Output:
         Y    - Smoothed X elements.
         Nsum - Number of not NaN's elements that fixed on the moving window.
                Provided to get a sum instead of a mean: Y.*Nsum.

       Description:
         Quickly smooths the matrix X by averaging each element along with
         the surrounding elements that fit in the little matrix
         (2Fr+1)x(2Fc+1) centered at the element (boxcar filter), but ignoring
         NaN's. The elements at the ends are also averaged but the ones on the
         corners are left intact. If Fr or Fc is zero or empty the smoothing
         is made through the columns or rows only, respectively. With the
         windows size defined in this way, the filter has zero phase.

       Example:
          [X,Y] = meshgrid(-2:.2:2,3:-.2:-2);
          Zi = 5*X.*exp(-X.^2-Y.^2);
          Zr = Zi + rand(size(Zi));
          Zr([8 46 398 400]) = NaN;
          Zs = nanmoving_average2(Zr,2,3);
           subplot(131), surf(X,Y,Zi)
           view(2), shading interp, xlabel('Z')
           subplot(132), surf(X,Y,Zr)
           view(2), shading interp, xlabel('Z + noise + NaN''s')
           subplot(133), surf(X,Y,Zs)
           view(2), shading interp, xlabel('Z smoothed')

       See also FILTER2, RECTWIN, NANMEAN and MOVING_AVERAGE,
       NANMOVING_AVERAGE, MOVING_AVERAGE2 by Carlos Vargas

## CROSS-REFERENCE INFORMATION

This function calls:

- moving_average MOVING_AVERAGE Smooths a vector through the moving average method.
- nanmoving_average NANMOVING_AVERAGE Moving average ignoring NaN's.

This function is called by:

## SOURCE CODE

    0001 function [X,A] = nanmoving_average2(X,Fr,Fc,INT)
    0002 %NANMOVING_AVERAGE2   Smooths a matrix through the moving average method.
    0003 %
    0004 %   Syntax:
    0005 %     [Y,Nsum] = nanmoving_average2(X,Fr,Fc,INT);
    0006 %
    0007 %   Input:
    0008 %     X   - Matrix of finite elements.
    0009 %     Fr  - Window semi-length in the rows. A positive scalar (default 0).
    0010 %     Fc  - Window semi-length in the columns. A positive scalar (default
    0011 %           Fr).
    0012 %     INT - If INT=0: do not interpolates NaN's (default); elseif INT=1 do
    0013 %           interpolates.
    0014 %
    0015 %   Output:
    0016 %     Y    - Smoothed X elements.
    0017 %     Nsum - Number of not NaN's elements that fixed on the moving window.
    0018 %            Provided to get a sum instead of a mean: Y.*Nsum.
    0019 %
    0020 %   Description:
    0021 %     Quickly smooths the matrix X by averaging each element along with
    0022 %     the surrounding elements that fit in the little matrix
    0023 %     (2Fr+1)x(2Fc+1) centered at the element (boxcar filter), but ignoring
    0024 %     NaN's. The elements at the ends are also averaged but the ones on the
    0025 %     corners are left intact. If Fr or Fc is zero or empty the smoothing
    0026 %     is made through the columns or rows only, respectively. With the
    0027 %     windows size defined in this way, the filter has zero phase.
    0028 %
    0029 %   Example:
    0030 %      [X,Y] = meshgrid(-2:.2:2,3:-.2:-2);
    0031 %      Zi = 5*X.*exp(-X.^2-Y.^2);
    0032 %      Zr = Zi + rand(size(Zi));
    0033 %      Zr([8 46 398 400]) = NaN;
    0034 %      Zs = nanmoving_average2(Zr,2,3);
    0035 %       subplot(131), surf(X,Y,Zi)
    0036 %       view(2), shading interp, xlabel('Z')
    0037 %       subplot(132), surf(X,Y,Zr)
    0038 %       view(2), shading interp, xlabel('Z + noise + NaN''s')
    0039 %       subplot(133), surf(X,Y,Zs)
    0040 %       view(2), shading interp, xlabel('Z smoothed')
    0041 %
    0042 %
    0043 %   See also FILTER2, RECTWIN, NANMEAN and MOVING_AVERAGE,
    0044 %   NANMOVING_AVERAGE, MOVING_AVERAGE2 by Carlos Vargas
    0045
    0046 % Copyright 2006-2008  Carlos Vargas, nubeobscura@hotmail.com
    0047 %    $Revision: 3.1 $  $Date: 2008/03/12 17:20:00 $
    0048
    0049 %   Written by
    0050 %   M. in S. Carlos Adrián Vargas Aguilera
    0051 %   Physical Oceanography PhD candidate
    0052 %   CICESE
    0053 %   Mexico,  march 2008
    0054 %
    0055 %   nubeobscura@hotmail.com
    0056 %
    0057 %   Download from:
    0058 %   http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objec
    0059 %   tType=author&objectId=1093874
    0060
    0061 % January 2008, fixed bug on the Fr,Fc data.
    0062
    0063 %% Error checking:
    0064 if ~nargin
    0065   error('Nanmoving_average2:Inputs','There are no inputs.')
    0066 elseif nargin<2 || isempty(Fr)
    0067  Fr = 0;
    0068 end
    0069 if ndims(X) ~= 2
    0070  error('Nanmoving_average2:Inputs','Entry must be a matrix.')
    0071 end
    0072 if nargin<3 || isempty(Fc)
    0073  Fc = Fr;
    0074 end
    0075 if nargin<4 || isempty(INT)
    0076  INT = 0;
    0077 end
    0078 if ~INT
    0079  inan = isnan(X);
    0080 end
    0081
    0082 %% MAIN
    0083 % Sums by each column with NaN interpolation:
    0084 [X,A] = nanmoving_average(X,Fr,1,1); X = X.*A;
    0085 % Sums by each sumed row:
    0086 [X,B] = nanmoving_average(X,Fc,2,1); X = X.*B;
    0087 % Sumed elements:
    0088 [A,C] =    moving_average(A,Fc,2);   A = A.*C;
    0089 % All NaNs
    0090 X(A==0) = NaN;
    0091 % Mean:
    0092 X = X./A;
    0093 % Interpolation?:
    0094 if ~INT
    0095  X(inan) = NaN;
    0096 end
    0097
    0098 % Carlos Adrián Vargas Aguilera. nubeobscura@hotmail.com