gpt4 book ai didi

arrays - 在 accumarray 中使用百分比函数

转载 作者:太空宇宙 更新时间:2023-11-03 19:31:47 25 4
gpt4 key购买 nike

我有两个数组:

OTPCORorder = [61,62,62,62,62,62,62,62,62,62,62,62,65,65,...]
AprefCOR = [1,3,1,1,1,1,1,1,1,1,2,3,3,2,...]

对于 OTPCORorder 中的每个元素,AprefCOR 中都有对应的元素。我想知道每组唯一 OTPCORorder 的数字 1 的百分比如下:

OTPCORorder1 = [61,62,65,...]
AprefCOR1 = [1,0.72,0,...]

我已经有了这个:

[OTPCORorder1,~,idx] = unique(OTPCORorder,'stable');
ANS = OTPCORorder1 = [61,62,65,...];

我曾经使用“accumarray”,但我使用的是“mean”或“sum”函数,例如:

AprefCOR1 = accumarray(idx,AprefCOR,[],@mean).';

我只是想知道是否存在一种使用此方法的方法,但使用“prctile”函数或任何其他函数可以给我特定元素的百分比,例如在这种情况下为“1”。

非常感谢。

最佳答案

这可能是一种方法:

%// make all those non-zero values to zero
AprefCORmask = AprefCOR == 1;

%// you have done this
[OTPCORorder1,~,idx] = unique(OTPCORorder,'stable');

%// Find number of each unique values
counts = accumarray(idx,1);

%// Find number of ones for each unique value
sumVal = accumarray(idx,AprefCORmask);

%// find percentage of ones to get the results
perc = sumVal./counts

结果:

输入:

OTPCORorder = [61,62,62,62,62,62,62,62,62,62,62,62,65,65];
AprefCOR = [1,3,1,1,1,1,1,1,1,1,2,3,3,2];

输出:

perc =

1.0000
0.7273
0

关于arrays - 在 accumarray 中使用百分比函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com