Average elements of an array between 2 percentiles

后端 未结 1 1384
孤城傲影
孤城傲影 2021-01-25 21:29

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 wa

相关标签:
1条回答
  • 2021-01-25 21:42

    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);
    
    0 讨论(0)
提交回复
热议问题