问题
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 :)
回答1:
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));
回答2:
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)
.
来源:https://stackoverflow.com/questions/7911052/pearson-correlation-without-using-zero-element-in-matlab