X = [1 2 3 4 5; 2 4 6 8 10]; covMatrix = cov(X'); disp(covMatrix);
The covariance matrix shows how two variables vary together. Here, the first variable is [1 2 3 4 5] and the second is [2 4 6 8 10]. The covariance matrix is symmetric and the off-diagonal elements represent covariance between the two variables.
A = [1 2 3 4 5]; B = [2 4 6 8 10]; r = corrcoef(A, B); disp(r(1,2));
Since B is exactly 2 times A, the correlation coefficient is 1, indicating perfect positive linear correlation.
X = [1 2 3 4 5; 2 4 6 8 10]; covMatrix = cov(X); disp(covMatrix);
In MATLAB, cov treats rows as observations and columns as variables. Since X has 2 rows and 5 columns, cov(X) treats 2 observations of 5 variables, resulting in a 5x5 covariance matrix.
data = [1 2 3; 4 5 6; 7 8 9]; corrMatrix = corrcoef(data); disp(corrMatrix);
corrcoef computes the correlation coefficients between columns of the input matrix. Here, data has 3 columns, so the output is a 3x3 matrix showing correlations between these columns.
X = [1 NaN 3 4 5; 2 4 NaN 8 10]; covMatrix = cov(X', 'partialrows'); disp(covMatrix);
The 'partialrows' option tells cov to use all available pairs of data ignoring NaNs. This results in a covariance matrix computed from partial data, giving the shown values.
