To calculate the correlation of two vectors in Matlab, you can use the built-in function corrcoef()
. This function takes two vector inputs, computes the correlation coefficient matrix, and returns it.
Here is an example code snippet to show how to calculate the correlation coefficient between two vectors x
and y
:
main.m172 chars9 lines
Output:
main.m56 chars2 lines
In this example, we first define two vectors x
and y
. We then use the corrcoef()
function to compute their correlation coefficient matrix, corr_mat
. Since we are interested in the correlation coefficient between x
and y
, we extract the value at the (1,2) index of corr_mat
, which is the correlation coefficient between the first and second input variables (x
and y
). Finally, we display the computed correlation coefficient using disp()
.
gistlibby LogSnag