I would prefer to exclude pairs with NaN from the correlation instead of introducing zeros. In this case, I would use the following code in matlab based on corr (Pearson autocorrelation coefficients).
out=zeros(nlags,1); out(1)=1; for i=2:nlags+1 out(i)=corr(data(i:end),data(1:end-i+1),'rows','complete'); end stem(0:nlags,out) title('sample ACF')
Hope this helps
Fabio
source share