Matlab中不使用零元素的皮尔逊相关

发布于 2024-12-12 01:26:32 字数 948 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩世 2024-12-19 01:26:33

我认为这是一个单行: 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 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).

吲‖鸣 2024-12-19 01:26:32

您必须首先获取好数据所在位置的索引:

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

pearson = corr(A(goodData),B(goodData));

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文