数组中 2 个百分位之间的平均元素

2024-05-01

我有 2 个长度为 200 的向量,例如 A 和 B;然后我使用以下方法找到数组 A 的每第二个百分位数 A1=prctile(A, [1:2:100],1); 这样A1是一个长度为50的数组。现在我想找到A1中每两个元素内的A元素的平均值(即A的第2个和第4个百分位数之间的元素的平均值)并对相应的元素求平均值B.


我的想法是,以良好的性能解决这个问题,创建一个“标签”向量,为 50 个百分位定义的每个 bin 分配一个标签(1 到 51 之间的数字)。

%Get the indices where each new percentile bin starts
[sortedA,indexA]=sort(A);
sortedB=B(indexA);
percentile_indices=ceil(prctile(1:numel(A), [1:2:100]));
label=zeros(size(A));
label([1,percentile_indices])=1;
%Convert this to a vector which assigns an index to each bin
label=cumsum(label);
%accumulate. Take the mean of all values with the same label.
prctileMeanA=accumarray(label(:),sortedA,[],@mean);
prctileMeanB=accumarray(label(:),sortedB,[],@mean);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

数组中 2 个百分位之间的平均元素 的相关文章

随机推荐