To calculate the moving covariance of two vectors in MATLAB, you can use the movcov
function. The syntax for this function is as follows:
main.m20 chars2 lines
where X
and Y
are the two vectors for which you want to calculate the moving covariance, and k
is the size of the moving window.
Here's an example:
main.m152 chars7 lines
In this example, C
will be a vector of length 96 (since the window size is 5, the first 4 and last 4 elements of the covariance vector will be NaN). Each element of C
represents the covariance between X
and Y
over a window of 5 elements.
Note that you can also use the movvar
function to calculate the moving variance of a vector, which may be useful in conjunction with movcov
to calculate the moving correlation between two vectors.
gistlibby LogSnag