Matlab中不使用零元素的皮尔逊相关
我在 Matlab 中有 2 个示例向量:
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];
当,我尝试用手动方法计算皮尔逊相关性并用excel来做 我有相同的结果 (0.667)
1 0,667 0,667 1
但是当我用简单的代码在 MatLab 中尝试时:
pearson = corr(A',B');
它返回具有不同分数的结果(0,2139)。
1 0,2139 0,2139 1
也许它的发生是因为使用零分数(0)来计算它。发生这种情况是因为在 matlab 中缺失值将被零(0)替换。
在皮尔逊相关性中,仅使用共同评级值来计算它。 (见粗体值)
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];
或者它可以变得简单:
A = [5,4,5,5,4< /b>]; B = [1,1,4,4,1];
有谁知道如何为此编写简单的代码? 我已经在程序代码中尝试过: 首先,使函数corated,average_corated,然后最后计算相似度。 这花费了太多时间。
先谢谢了:)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是一个单行:
pearson = corr(A(B ~= 0)', B(B ~= 0)')
B ~= 0
创建一个大小为size(B)
的二进制矩阵,如果B
中的相应条目不为零,则为1
,并且0
> 否则。您还可以使用相同大小的二进制矩阵对矩阵进行索引,因此如果size(A) == size(B)
,这应该始终有效。I think this is a one liner:
pearson = corr(A(B ~= 0)', B(B ~= 0)')
B ~= 0
creates a binary matrix of sizesize(B)
which is1
if the corresponding entry inB
is not zero, and0
otherwise. You can also index into a matrix using binary matrices of the same size, so this should always work if thesize(A) == size(B)
.您必须首先获取好数据所在位置的索引:
You have to get the index for where the good data is located first: