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

Arrays 数组的平均元素在2个百分位之间,arrays,matlab,Arrays,Matlab,我有两个长度为200的向量,比如A和B;然后我使用 A1=比例(A,[1:2:100],1); 所以A1是一个长度为50的数组。现在,我想找出A1的两个元素中A元素的平均值(即A元素的平均值介于第2百分位和第4百分位之间)以及B的相应元素的平均值。我的想法是创建一个“标签”向量,分配一个标签(数字介于1和51之间)到50%定义的每个箱子 %Get the indices where each new percentile bin starts [sortedA,indexA]=sort(A);

我有两个长度为200的向量,比如A和B;然后我使用 A1=比例(A,[1:2:100],1);
所以A1是一个长度为50的数组。现在,我想找出A1的两个元素中A元素的平均值(即A元素的平均值介于第2百分位和第4百分位之间)以及B的相应元素的平均值。

我的想法是创建一个“标签”向量,分配一个标签(数字介于1和51之间)到50%定义的每个箱子

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

我的想法是创建一个“标签”向量,将一个标签(1到51之间的数字)分配给50%定义的每个箱子

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

谢谢你,丹尼尔,这解决了我问题的一部分,我实际上想得到对应于a的B的值,比如如果第2百分位和第4百分位内a的元素是[2,8,19],可能是长度为200的数组a中的第16、22和112个元素,那么我还需要向量B的第16、22和112个元素的和@Daniel@Sourangsu:更新了答案。谢谢Daniel,这解决了我的一部分问题,我实际上想得到对应于a的B值,比如如果第2百分位和第4百分位内a的元素是[2,8,19],这可能是长度为200的数组a中的第16、22和112个元素,然后我还需要向量B的第16、22和112个元素的和@Daniel@Sourangsu:更新了答案。