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