0

I have two vectors A & B, and both can be (1xn) matrix.

Will the below code still work: C=cov(x,y); p=C(2)/(std(x)*std(y));

user537670
  • 821
  • 6
  • 21
  • 42
  • 2
    possible duplicate of [Pearson's Coefficient and Covariance calculation in Matlab](http://stackoverflow.com/questions/5644981/pearsons-coefficient-and-covariance-calculation-in-matlab) – abcd Apr 26 '11 at 13:47

1 Answers1

1

I am not sure what you mean by "vectors [...] can be (1xn) matrix" ... aren't all vectors 1-by-n matrices? Do you mean 'as opposed to n-by-1'? Besides, what is keeping you from simply trying it?

>> x=rand(1,100);y=rand(1,100)+x;
>> C=cov(x,y); p=C(2)/(std(x)*std(y))

 p =

     0.6642

Looks about right to me...

Alternatively, try this: help corrcoef

arne.b
  • 4,212
  • 2
  • 25
  • 44
  • i have x = [1 0 1 0 0 0]; y = [1 1 1 1 1 1]; the value of C(2) is zero in this case. Any idea where I am going wrong – user537670 Apr 28 '11 at 06:28
  • Nowhere. There is 0 covariance between x and y. (Also, there is 0 variance in y, but this is not a necessary condition. There is also 0 covariance between x and [0 1 1 1 0 0], for example.) – arne.b Apr 28 '11 at 08:38