在Matlab中不使用零元素的Pearson相关

在Matlab中不使用零元素的Pearson相关

本文介绍了在Matlab中不使用零元素的Pearson相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中有2个示例向量:

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];

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 )

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

1 0,6670,667 1

1 0,6670,667 1

但是当我在MatLab中尝试使用简单代码时:

But when I tried in MatLab with simple code:

pearson = corr(A',B');

它以不同的分数( 0,2139 )返回结果.

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

1 0,2139 0,2139 1

1 0,2139 0,2139 1

可能是因为零得分(0)用于计算它而发生.之所以发生,是因为缺失的值将在matlab中被零(0)替换.

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.

在Pearson Correlation中,仅使用协定值来计算它. (请参见粗体值)

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];

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 = [ 1 1 4 4 1 ];

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

有人知道吗,如何为此编写简单的代码?我已经在程序代码中尝试过:首先,使函数为corated,average_corated,然后最后计算相似度.花了太多时间.

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.

感谢:)

推荐答案

您必须获取良好数据首先位于何处的索引:

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));

这篇关于在Matlab中不使用零元素的Pearson相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:16