0

I have 2 example vector in Matlab :

A = [5,3,3,0,4,1,5,0,2,5,5,0,5,3,4,0,1,4,4,0,4,2]; B = [1,0,0,0,1,0,4,0,0,0,0,4,4,0,1,0,0,0,0,0,0,0];

When, I try to calculate pearson correlation with manual method and do it with excel I have the same result (0.667)

1 0,667 0,667 1

But when I tried in MatLab with simple code:

pearson = corr(A',B');

it return the result with different score (0,2139).

1 0,2139 0,2139 1

Maybe Its happen because the zero score(0) is using to calculate it. In happen because the missing value will be replace by zero(0) in matlab.

In Pearson Correlation that only use co-rated value to calculate it. (see the bold value)

A = [5,3,3,0,4,1,5,0,2,5,5,0,5,3,4,0,1,4,4,0,4,2]; B = [1,0,0,0,1,0,4,0,0,0,0,4,4,0,1,0,0,0,0,0,0,0];

or it can make simple :

A = [5,4,5,5,4]; B = [1,1,4,4,1];

Does anyone know, how to make simple code for this? I have try it in procedural code : first, make function corated, average_corated, then at last calculate similarity. it cost too much time.

Thanks before :)

2 Answers2

3

You have to get the index for where the good data is located first:

goodData = A~=0 & B~=0; %# true wherever there's data for both A and B

pearson = corr(A(goodData),B(goodData));
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • wow, thanks a lot.maybe you forgot to use (') pearson = corr(A(goodData)',B(goodData)'); because corr only used for calculate column size. thanks a lot again. – user413262 Oct 27 '11 at 02:55
  • may I ask you 1 question again? when it become 1 matrices (A = [5,3,3,0,4,1,5,0,2,5,5,0,5,3,4,0,1,4,4,0,4,2; 1,0,0,0,1,0,4,0,0,0,0,4,4,0,1,0,0,0,0,0,0,0]; how to use it? – user413262 Oct 27 '11 at 03:00
  • @user413262: then, you write: `goodData = any(A,1);pearson=corr(A(1,goodData),A(2,goodData));` – Jonas Oct 27 '11 at 03:12
  • hmm..maybe i use this : goodData = A(:,1)~=0 & A(:,2)~=0; X=corr(A(goodData,1),A(goodData,2)); – user413262 Oct 28 '11 at 02:10
  • I have a problem again Jonas. Can you help me? when I used m x n matrices (m,n>3). I cant get right correlation for each column (ex : column 1 and column 2, column 3 and column 4, etc). here is my code : size_matrix = size(training_matrix); size_user_matrix = size_matrix(1);A=training_matrix'; for u=1 : size_user_matrix for v=1 : size_user_matrix goodData(u,v) = A(:,u)~=0 & A(:,v)~=0; pearson(u,v)=corr(A(goodData,u),A(goodData,v)); end end – user413262 Oct 28 '11 at 07:33
0

I think this is a one liner: pearson = corr(A(B ~= 0)', B(B ~= 0)')

B ~= 0 creates a binary matrix of size size(B) which is 1 if the corresponding entry in B is not zero, and 0 otherwise. You can also index into a matrix using binary matrices of the same size, so this should always work if the size(A) == size(B).

yuzeh
  • 442
  • 1
  • 5
  • 12
  • thanks before for answering :) but when I used it in MatLab it still dont get the answer : A(B ~= 0)' = have 6 value B(A ~= 0)' = have 17 value . its should be both of them have the same value and dont have zero score on them :) btw, thanks again :) – user413262 Oct 27 '11 at 02:45