2百分之间的阵列的平均元素 [英] Average elements of an array between 2 percentiles

查看:118
本文介绍了2百分之间的阵列的平均元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有长200 2载体,说A和B;然后我发现阵列中的每一秒百分位使用
A1 = prctile(A,[1:2:100],1);
使A1为长度50的阵列现在我想找到落在A1(即平均第2次和第4次百分之间的元素)的每两个元素内的A的元素的平均,也平均了相应的元件B的

I have 2 vectors of length 200, say A and B; then I find every second percentile of the array A using A1=prctile(A, [1:2:100],1); so that A1 is an array of length 50. Now I want to find the average of the elements of A that fall within each two elements of A1( ie. average of elements of A between 2nd and 4th percentile) and also average the corresponding elements of B.

推荐答案

我的想法与良好的性能是创建于每个定义的分区中的一个标签载体,其分配(1至51号)的标签解决这个由50百分位。

My idea to solve this with a good performance is creating a 'label' vector which assigns a label (number between 1 and 51) to each of the bins defined by the 50 percentiles.

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

这篇关于2百分之间的阵列的平均元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆