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.
% NANCOV.M
% 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
NmatB=~isnan(B);
A(isnan(A))=0; % Replace NaNs in A,B, and counter matrices
B(isnan(B))=0; % with zeros
Npts=NmatA*NmatB;
C=(A*B)./Npts;
% NANEOF.M
% 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
Cov=nancov(anom,anom');
%-----------------------------------------------------------------------
% 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
anom(isnan(anom))=0;
amps=B'*anom; % amplitudes
%-----------------------------------------------------------------------
% Checks
%check=B*amps; % Check that this gives original
% ANOM matrix