# moving_average2

## PURPOSE

MOVING_AVERAGE2 Smooths a matrix through the moving average method.

## SYNOPSIS

function [X,A] = moving_average2(X,Fr,Fc)

## DESCRIPTION

    MOVING_AVERAGE2   Smooths a matrix through the moving average method.

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

       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).

       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). 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));
          Zs = moving_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')
           subplot(133), surf(X,Y,Zs)
           view(2), shading interp, xlabel('Z smoothed')

       See also FILTER2, RECTWIN and MOVING_AVERAGE, NANMOVING_AVERAGE,
       NANMOVING_AVERAGE2 by Carlos Vargas

## CROSS-REFERENCE INFORMATION

This function calls:

- moving_average MOVING_AVERAGE Smooths a vector through the moving average method.

This function is called by:

## SOURCE CODE

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