Home > atmlab > math > stdnans.m

stdnans

PURPOSE ^

STDNANS Take the mean removing first NaNs

SYNOPSIS ^

function [ m, s ] = stdnans( n )

DESCRIPTION ^

 STDNANS   Take the mean removing first NaNs

    As matlab STD but removing first the NaNs. If a column is
    all NaNs the mean of that column and the number of values
    used for the average will be flagged as NaN

 FORMAT   [ m, s ] = stdnans( n )
        
 OUT   m      std of the vector or vector with a mean of the columns 
              of the matrix
       s      number of values used for each mean after NaNs are
              removed
 IN    n      Vector or matrix

 2009-5-20  Created by Salomon Eliasson.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

stdnans.m

SOURCE CODE ^

0001 % STDNANS   Take the mean removing first NaNs
0002 %
0003 %    As matlab STD but removing first the NaNs. If a column is
0004 %    all NaNs the mean of that column and the number of values
0005 %    used for the average will be flagged as NaN
0006 %
0007 % FORMAT   [ m, s ] = stdnans( n )
0008 %
0009 % OUT   m      std of the vector or vector with a mean of the columns
0010 %              of the matrix
0011 %       s      number of values used for each mean after NaNs are
0012 %              removed
0013 % IN    n      Vector or matrix
0014 %
0015 % 2009-5-20  Created by Salomon Eliasson.
0016 
0017 
0018 function [ m, s ] = stdnans( n )
0019 
0020 
0021 % making sure is a column vector
0022 if isvector( n ) && ( size(n,1) < size(n,2) )
0023   n   =  n';
0024 end
0025 
0026 % doing std per column after removing NaNs
0027 m  = zeros( 1, size( n, 2 ) );
0028 s  = zeros( size(m) );
0029 for j = 1:size( n, 2 )
0030    a    = n( :, j );
0031    a    = a( ~isnan( a ) );
0032    if isempty( a ) 
0033      m(j)   = NaN;
0034      s(j)   = NaN;
0035    else
0036      m(j)   = std( a );
0037      s(j)   = length( a );
0038    end
0039 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005