function [bin]=doBin(NL_x,NL_y,xbin) %this subfunction is doing the binning for nonLinearity %get different bins %% 1. get equal amount of observation inside a bin %standard score: x=prctile(NL_x,linspace(0,100,xbin+1)); %you want equal amount of observations in each bin!And not one bin of only e.g. 10 observations. %I want to now where the percentil are %prctile gives you now where the edges of the corresponding percentile rank %are. x(end)=x(end)+x(end)/100; %the last bin should be a bit bigger so that the last observation %can also go inside it [observations,indx]=histc(NL_x,x); %little trick so that you have 40 equal bin and not one bin at the end %that is empty or just has one value observations=observations(1:end-1); %make average in y axis NL_ytemp=cell(1,length(observations)); for mm=1:length(NL_ytemp) NL_ytemp{mm}=NL_y(indx==mm); end %make average in x axis x_AVG=cell(1,length(observations)); for mm=1:length(x_AVG) x_AVG{mm}=NL_x(indx==mm); end %get the average values per bin bin.NL_ybin=cellfun(@nanmean,NL_ytemp); bin.NL_xbin=cellfun(@nanmean,x_AVG); end