using Corrcoef in vector
Show older comments
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?
Answers (1)
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!
Categories
Find more on Correlation and Convolution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!