Q: How to ignore NaN's in my data?

Missing data (or NaN's in matrices) is sometimes a big problem. MATLAB has a few functions to deal with this situation: NANMEAN, NANMEDIAN, NANSTD, NANMIN, NANMAX, NANSUM. Below are additional functions (© Kara Lavender), that compute covariance matrix and EOFs from incomplete data.

% Program to compute a covariance matrix ignoring NaNs
% Usage: C = nancov(A,B)
% NANCOV calculates the matrix product A*B ignoring NaNs by replacing them
% with zeros, and then normalizing each element C(i,j) by the number of 
% non-NaN values in the vector product A(i,:)*B(:,j).
% A - left hand matrix to be multiplied
% B - right hand matrix to be multiplied
% C - resultant covariance matrix
% Example: A = [1 NaN 1] , B = [1
%                               1
%                               1]
% then nancov(A,B) is 2/2 = 1
% This is much better than Josh's stupid way (his words, not mine).

function [C]=nancov(A,B);

NmatA=~isnan(A); % Counter matrix

A(isnan(A))=0; % Replace NaNs in A,B, and counter matrices
B(isnan(B))=0; % with zeros


% Function to compute EOFs from demeaned data with NaNs:
% function [B,vars,amps]=naneof(anom);
% Computes EOFs of ROW DIMENSION in anom matrix

function [B,vars,amps]=naneof(anom);

% Compute covariance of matrix with NaNs

% Compute eigenvectors (EOFs), eigenvalues (variances of amplitudes) of
% data covariance matrix
[B,D]=eig(Cov);       % B = EOFs, diag(D) = eigenvalues
vars=flipud(diag(D)); % eigenvalues = variances of each amplitude

% EIG outputs from low to high, so flip column
% to order from high to low
B=fliplr(B);          % Flip columns left/right to correspond
                      % to adjusted ordering of amplitudes

% Put 0's in for NaNs in ANOM matrix to be able to compute amplitudes

amps=B'*anom;       % amplitudes

% Checks
%check=B*amps; % Check that this gives original
% ANOM matrix