doBin.m 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. function [bin]=doBin(NL_x,NL_y,xbin)
  2. %this subfunction is doing the binning for nonLinearity
  3. %get different bins
  4. %% 1. get equal amount of observation inside a bin
  5. %standard score:
  6. 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.
  7. %I want to now where the percentil are
  8. %prctile gives you now where the edges of the corresponding percentile rank
  9. %are.
  10. x(end)=x(end)+x(end)/100; %the last bin should be a bit bigger so that the last observation
  11. %can also go inside it
  12. [observations,indx]=histc(NL_x,x);
  13. %little trick so that you have 40 equal bin and not one bin at the end
  14. %that is empty or just has one value
  15. observations=observations(1:end-1);
  16. %make average in y axis
  17. NL_ytemp=cell(1,length(observations));
  18. for mm=1:length(NL_ytemp)
  19. NL_ytemp{mm}=NL_y(indx==mm);
  20. end
  21. %make average in x axis
  22. x_AVG=cell(1,length(observations));
  23. for mm=1:length(x_AVG)
  24. x_AVG{mm}=NL_x(indx==mm);
  25. end
  26. %get the average values per bin
  27. bin.NL_ybin=cellfun(@nanmean,NL_ytemp);
  28. bin.NL_xbin=cellfun(@nanmean,x_AVG);
  29. end