使用百分比函数与accumarray

chi*_*_16 6 arrays matlab percentage accumarray

我有两个数组:

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,...]
Run Code Online (Sandbox Code Playgroud)

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

OTPCORorder1 = [61,62,65,...]
AprefCOR1 = [1,0.72,0,...]
Run Code Online (Sandbox Code Playgroud)

我已经有了这个:

[OTPCORorder1,~,idx] = unique(OTPCORorder,'stable');
ANS = OTPCORorder1 = [61,62,65,...];
Run Code Online (Sandbox Code Playgroud)

我曾经使用过"accumarray",但我使用了"mean"或"sum"这样的功能:

AprefCOR1 = accumarray(idx,AprefCOR,[],@mean).';
Run Code Online (Sandbox Code Playgroud)

我只是想知道是否存在一种方法来使用它,但是使用"prctile"函数或任何其他函数给出了特定元素的百分比,例如在这种情况下为"1".

非常感谢你.

San*_*lai 5

这可能是一种方法:

%// 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
Run Code Online (Sandbox Code Playgroud)

结果:

输入:

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];
Run Code Online (Sandbox Code Playgroud)

输出:

perc =

1.0000
0.7273
     0
Run Code Online (Sandbox Code Playgroud)