get_prctile.m 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. function y= get_prctile(datumM, distM)
  2. % p= stats.get_p(datum, dist)
  3. % p= stats.get_p(datum, dist, tails, high)
  4. % p is the percentile of datum in the distribution dist.
  5. % datum can be a single value or a 1 by n vector
  6. % dist can be a vector or an m by n matrix
  7. %
  8. % e.g. p= stats.get_prctile(2,randn(1000,1));
  9. %
  10. % p = 0.9799
  11. %
  12. % check that inputs are the right size
  13. if numel(datumM)~=size(distM,2)
  14. error('GET_P:BADINPUTS','Number of columns of distM must equal lenght of datumM')
  15. end
  16. y=ones(size(datumM));
  17. for dx=1:numel(datumM)
  18. datum=datumM(dx);
  19. dist=distM(:,dx);
  20. ps=[0.001:0.001:10 10.1:0.1:90 90.001:0.001:99.999];
  21. sd_ps=prctile(dist,ps);
  22. closest= stats.qfind(sd_ps,datum);
  23. if isnan(datum)
  24. % Then the value of from_uni was lower than any value in the
  25. % distribution.
  26. y(dx)=nan;
  27. else
  28. if closest<=0 % datum out of range
  29. others=1;
  30. else
  31. others=find(sd_ps==sd_ps(closest));
  32. end
  33. if ps(others(1))<50 && ps(others(end))>50
  34. % if the datum stradles the mean.
  35. sd_p=0.5;
  36. elseif ps(others(1))>50
  37. sd_p=ps(others(1))/100;
  38. else
  39. sd_p=ps(others(end))/100;
  40. end
  41. y(dx)=sd_p;
  42. end
  43. end