MATLAB: How to calculate moving covariance in a matrix

moving covariance

Hi guys, Hi Guys, I have got a matrix :378×9. I need to calculate the moving covariance with a window size of 120(starting from row one). Can somebody help me please? Thank you very much Andrea

Best Answer

% Generate some pretend data. Use your real data here.
data = rand(378,9);
[M,N] = size(data);
window = 120;
numberCovarianceMatrices = M - window + 1;
covarianceMatrices = zeros(N,N,numberCovarianceMatrices);
for nc = 1:numberCovarianceMatrices
covarianceMatrices(:,:,nc) = cov(data(nc:(nc+window-1),:));
end