In MATLAB, the corrcoef()
function can be used to compute the correlation matrix for two input vectors. The r^2 value is the square of the correlation coefficient (r). Here is an example code to calculate and display the r^2 value given two vectors x and y:
main.m245 chars13 lines
Output:
main.m18 chars2 lines
Note that the corrcoef function assumes that the input vectors are columns of data, so if you have row vectors, you may need to transpose them using the transpose operator ' (single quote) before passing them to corrcoef.
gistlibby LogSnag