Correlation between two vectors?

前端 未结 4 1669
轻奢々
轻奢々 2021-02-18 18:54

I have two vectors:

A_1 = 

      10
      200
      7
      150

A_2 = 
      0.001
      0.450
      0.0007
      0.200

I would like to know

4条回答
  •  执念已碎
    2021-02-18 19:39

    Given:

    A_1 = [10 200 7 150]';
    A_2 = [0.001 0.450 0.007 0.200]';
    

    (As others have already pointed out) There are tools to simply compute correlation, most obviously corr:

    corr(A_1, A_2);  %Returns 0.956766573975184  (Requires stats toolbox)
    

    You can also use base Matlab's corrcoef function, like this:

    M = corrcoef([A_1 A_2]):  %Returns [1 0.956766573975185; 0.956766573975185 1];
    M(2,1);  %Returns 0.956766573975184 
    

    Which is closely related to the cov function:

    cov([condition(A_1) condition(A_2)]);
    

    As you almost get to in your original question, you can scale and adjust the vectors yourself if you want, which gives a slightly better understanding of what is going on. First create a condition function which subtracts the mean, and divides by the standard deviation:

    condition = @(x) (x-mean(x))./std(x);  %Function to subtract mean AND normalize standard deviation
    

    Then the correlation appears to be (A_1 * A_2)/(A_1^2), like this:

    (condition(A_1)' * condition(A_2)) / sum(condition(A_1).^2);  %Returns 0.956766573975185
    

    By symmetry, this should also work

    (condition(A_1)' * condition(A_2)) / sum(condition(A_2).^2); %Returns 0.956766573975185
    

    And it does.

    I believe, but don't have the energy to confirm right now, that the same math can be used to compute correlation and cross correlation terms when dealing with multi-dimensiotnal inputs, so long as care is taken when handling the dimensions and orientations of the input arrays.

提交回复
热议问题