using Corrcoef in vector

5 次查看(过去 30 天)
Mah Mhata
Mah Mhata 2019-7-3
编辑: Satwik 2025-4-23
I have 2 vector which sizes are 1*100 .
I want to compute correlation between all vector elements, but as you know corrcoef just show 2*2 matrix. not for all of the number, whoat should I do?

回答(1 个)

Satwik
Satwik 2025-4-23
编辑:Satwik 2025-4-23
If two vectors of size 1×100 are given, the correlation between corresponding elements is computed as a single correlation coefficient. The 'corrcoef' function returns a 2×2 matrix, where the off-diagonal element is the correlation value between the two vectors. To compute the correlation between all pairs of elements (i.e., a 100×100 matrix), a correlation matrix can be formed by combining the vectors into a 100×2 matrix, and then the correlations between all columns can be computed.
A = rand(1,100);
B = rand(1,100);
% Pairwise correlation matrix (each element: correlation between scalar pairs)
corrMat = (A' - mean(A)) * (B - mean(B)) / ((length(A)-1) * std(A) * std(B));
However, this will produce a matrix where each entry is the product of deviations, not standard correlation.
Kindly refer to the following documentation for detailed information on the 'corrcoef' function: https://www.mathworks.com/help/matlab/ref/corrcoef.html
I hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by