### How to find the correlation coefficient matrix using matlab

Use corr to solve.

A=rand(4,5);

RHO=corr(A)

%A is a 4×5 random matrix with 5 column vectors

%RHO is the correlation coefficient matrix of A, where each element is the correlation coefficient of each pair of column vectors in A

%For example, RHO(1,1) is the A ‘s first column and the correlation coefficient of the first column, with a value of 1

%RHO(1,2) is the correlation coefficient of the first column and the second column of A, and RHO(1,2) and RHO(2,1) are equal

%So RHO is a 5×5 matrix, and is symmetric!

### How MATLAB finds the correlation coefficient

Simply put, this is done with the corrcoef function.

This is the result of finding the correlation, for a general matrix X, after performing A=corrcoef(X), the row a and column b where each value in A is located responds to the degree of similarity (i.e., the correlation coefficient) between the corresponding ath column vector and bth column vector in the original matrix X. The formula is C(1,2)/SQRT(C(1,1)*C(2,2)), where C denotes the covariance matrix of the matrix [f,g], and assuming that f and g are both column vectors (both sequences must have the same length to participate in the operation), the resulting (the part we are interested in) is a number. Taking the default A=corrcoef(f,g) as an example, the output A is a two-dimensional matrix (with diagonal elements constant at 1), and the correlation coefficients of f and g are stored on A(1,2)=A(2,1), with values between [-1,1], with 1 denoting the largest positive correlation, and -1 denoting the largest negative correlation at the absolute value.

### How to calculate matrix correlation coefficient using matlab

The covariance matrix is known and calculating the correlation coefficient can be done by following the formula in the figure.

R is the correlation coefficient matrix and C is the covariance matrix.

>>a=rand(5,5)

a=

0.95010.76210.61540.40570.0579

0.23110.45650.79190.93550.3529

0.60680. 01850.92180.91690.8132

0.48600.82140.73820.41030.0099

0.89130.44470.17630.89360.1389

>>C=cov(a)

C=

0.08780.0129-0.0526-0.0253-0.0276

0.01290.1022-0.0229-0.0739-0.0993

-0.0526-0.02290.0819-0.00370.0515

– 0.0253-0.0739-0.00370.07740.0624

-0.0276-0.09930.05150.06240.1079%%% covariance matrix

>>R=corrcoef(a)

R=

1.00000. 1364-0.6207-0.3063-0.2836

0.13641.0000-0.2503-0.8309-0.9454

-0.6207-0.25031.0000-0.04600.5478

-0.3063-0.8309- 0.04601.00000.6822

-0.2836-0.94540.54780.68221.0000%% Correlation Coefficient Matrix

It can be seen that the correlation coefficient matrix is is symmetric array. It is calculated that R(1,2) is the correlation coefficient of the first and second columns; R(1,3) is the correlation coefficient of the first and third columns; R(2,3) is the correlation coefficient of the second and third columns; R(1,2) and R(2,1) are both correlation coefficients of the first and second columns so they are equal.