MATLAB: Lagged/Cross Correlations with missing values

corrcoefcorrelationcross correlationlagmissing valuesnanpairwisexcorr

I need to compute the cross correlation (up to 7 days lag) for each column in my matrix. However, many of the cells contain missing data. The data is satellite data and the missing days mean it was too cloudy. Therefore, the cells with NaN cannot be turned into 0's, but instead must be included in the cross correlation because they are relevant information. However, the xcorr function does not include the 'pairwise' feature like the corrcoef function that omits rows with missing values. So more specifically, how do I calculation the cross correlation (xcorr) with a 7 day lag while omitting rows where an NaN is present. Here is an example of a small segment of data and the equation I am using so far.
Equation: [Cor, lag] = xcorr(A, B, 7, 'coeff');
A = [.2, .33, .4, .34, .56, NaN, .7, .9, .1, NaN]
B = [NaN, .1, .2, .3, NaN, NaN, .4, .5, .55, .34]

Best Answer

isOK=isfinite(A) & isfinite(B); % both rows finite (neither NaN)
[r,lag]=xcorr(A(isOK),B(isOK),'coeff');
Related Question