<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/291712</link>
    <title>MATLAB Central Newsreader - replicating MS Excel percentrank formula</title>
    <description>Feed for thread: replicating MS Excel percentrank formula</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2013 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.nl/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 16 Sep 2010 15:05:23 +0000</pubDate>
      <title>replicating MS Excel percentrank formula</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/291712#779994</link>
      <author>Naresh Pai</author>
      <description>I am trying to replicate MS Excel's percentrank function in Matlab.&lt;br&gt;
&lt;br&gt;
% example data&lt;br&gt;
data =  [1.1 1.2 2.1 3.5 6.1 1.2 5.1 5.2 9.1 7.1]'&lt;br&gt;
&lt;br&gt;
% excel percentrank results are:&lt;br&gt;
% [0.00 0.11 0.33 0.44 0.77 0.11 0.55 0.66 1.00 0.88]'&lt;br&gt;
&lt;br&gt;
% Mathworks website suggests using the following formula to calculate percentile rank&lt;br&gt;
&lt;br&gt;
prctile_rank = tiedrank(data)/length(data)&lt;br&gt;
&lt;br&gt;
% and the result is:&lt;br&gt;
prctile_rank = [0.10 0.25 0.40 0.50 0.80 0.25 0.60 0.70 1.00 0.90]'&lt;br&gt;
&lt;br&gt;
% Too bad. So, I try this:&lt;br&gt;
&lt;br&gt;
% clear previous variables&lt;br&gt;
clear all&lt;br&gt;
&lt;br&gt;
% include serial number for sorting later&lt;br&gt;
data(:,1) = 1:10;&lt;br&gt;
&lt;br&gt;
% example data&lt;br&gt;
data(:,2) = [1.1 1.2 2.1 3.5 6.1 1.2 5.1 5.2 9.1 7.1]';&lt;br&gt;
&lt;br&gt;
% sort the array using column 2&lt;br&gt;
[x, i] = sort(data(:,2));&lt;br&gt;
&lt;br&gt;
% store sorted array&lt;br&gt;
data_sorted = data(i,:);&lt;br&gt;
&lt;br&gt;
%loop through&lt;br&gt;
for i = 1:length(data)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% find number of values less than&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;less_than = numel(find(data_sorted(:,2)&amp;lt;data_sorted(i,2)));&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% percentile rank&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data_sorted(i,3) = floor((less_than)*10/length(data))/10;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% clear temp variable&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;clear less_than&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
% put it back together&lt;br&gt;
[y, j] = sort(data_sorted(:,1));&lt;br&gt;
prctile_rank = data_sorted(j,3)&lt;br&gt;
&lt;br&gt;
% the answer is:&lt;br&gt;
prctile_rank = [0.00 0.10 0.30 0.40 0.70 0.10 0.50 0.60 0.90 0.80]&lt;br&gt;
&lt;br&gt;
% close but not good enough. especially the last but one differs by 0.1&lt;br&gt;
&lt;br&gt;
Can somebody suggest a better way of doing this?</description>
    </item>
    <item>
      <pubDate>Thu, 16 Sep 2010 16:45:49 +0000</pubDate>
      <title>Re: replicating MS Excel percentrank formula</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/291712#780028</link>
      <author>Ashish Uthama</author>
      <description>Naresh,&lt;br&gt;
&lt;br&gt;
In&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;gt;    % percentile rank&lt;br&gt;
&amp;nbsp;&amp;gt;    data_sorted(i,3) = floor((less_than)*10/length(data))/10;&lt;br&gt;
&lt;br&gt;
What do you really want the denominator to be? the number of elements or &lt;br&gt;
the max value of the data?&lt;br&gt;
if you replace it with:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% percentile rank based on max value&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data_sorted(i,3) = ((less_than)/max(data(:,2)));&lt;br&gt;
&lt;br&gt;
You might get what you seem to want.&lt;br&gt;
&lt;br&gt;
(Note: I think excel rounds the answer to two decimal digits).</description>
    </item>
    <item>
      <pubDate>Thu, 16 Sep 2010 19:04:08 +0000</pubDate>
      <title>Re: replicating MS Excel percentrank formula</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/291712#780075</link>
      <author>Naresh Pai</author>
      <description>Ashish Uthama &amp;lt;first.last@mathworks.com&amp;gt; wrote in message &amp;lt;i6thju$ebg$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Naresh,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; In&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  &amp;gt;    % percentile rank&lt;br&gt;
&amp;gt;  &amp;gt;    data_sorted(i,3) = floor((less_than)*10/length(data))/10;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What do you really want the denominator to be? the number of elements or &lt;br&gt;
&amp;gt; the max value of the data?&lt;br&gt;
&amp;gt; if you replace it with:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      % percentile rank based on max value&lt;br&gt;
&amp;gt;      data_sorted(i,3) = ((less_than)/max(data(:,2)));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You might get what you seem to want.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (Note: I think excel rounds the answer to two decimal digits).&lt;br&gt;
&lt;br&gt;
Ashish,&lt;br&gt;
&lt;br&gt;
Thanks for your reply. I tried the formula you suggested above. Here are the percentile ranks I get:&lt;br&gt;
&lt;br&gt;
prctile_rank = [0 0.1099 0.3297 0.4396 0.7692 0.1099 0.5495 0.6593 0.9890 0.8791]'&lt;br&gt;
&lt;br&gt;
With excel the values are (I have customized the cells in excel to show four digits after decimal point) :&lt;br&gt;
&lt;br&gt;
[0.0000 0.1110 0.3330 0.4440 0.7770 0.1110 0.5550 0.6660 1.000 0.8888]'&lt;br&gt;
&lt;br&gt;
Although the values are fairly close, they are not good enough for me. The issue is that I am using these numbers in another formula with a large multiplier. This increases the error by at least 2 to 3 orders of magnitude. Hence, I need to exactly replicate what excel does with its percentrank function.&lt;br&gt;
&lt;br&gt;
In percentile rank formula, I am trying divide less_than by the number of elements in the data. In this example, I have used 10. &lt;br&gt;
&lt;br&gt;
I think the issue comes when we have two elements with same values in the array. I am trying to figure how excel handles this.&lt;br&gt;
&lt;br&gt;
Let me know if this is not clear. I have provided a small example here to simplify the question. I am working with a larger data set. I can send that to you and the code, if necessary. &lt;br&gt;
&lt;br&gt;
Thanks again!&lt;br&gt;
&lt;br&gt;
Naresh</description>
    </item>
    <item>
      <pubDate>Thu, 16 Sep 2010 19:32:21 +0000</pubDate>
      <title>replicating MS Excel percentrank formula</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/291712#780089</link>
      <author>Roger Stafford</author>
      <description>"Naresh Pai" &amp;lt;npai@uark.edu&amp;gt; wrote in message &amp;lt;i6tbnj$94h$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I am trying to replicate MS Excel's percentrank function in Matlab.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % example data&lt;br&gt;
&amp;gt; data =  [1.1 1.2 2.1 3.5 6.1 1.2 5.1 5.2 9.1 7.1]'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % excel percentrank results are:&lt;br&gt;
&amp;gt; % [0.00 0.11 0.33 0.44 0.77 0.11 0.55 0.66 1.00 0.88]'&lt;br&gt;
&amp;gt; ..........&lt;br&gt;
&amp;gt; Can somebody suggest a better way of doing this?&lt;br&gt;
- - - - - - - -&lt;br&gt;
&amp;nbsp;&amp;nbsp;Here is my best guess as to the logic in MS Excel's percentrank function, based on your single example.  As in that example, let data be a column vector.  Then do this:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;[x,p] = sort(data);   % Put data in ascending order&lt;br&gt;
&amp;nbsp;x = [true;diff(x)~=0;true];   % Check for repetitions&lt;br&gt;
&amp;nbsp;t = diff(x);   % Check for beginnings and endings of repetitions series&lt;br&gt;
&amp;nbsp;f = find(t==-1);   % Beginnings of repetition series&lt;br&gt;
&amp;nbsp;g = find(t==+1)+1;   % Next addresses after their ends&lt;br&gt;
&amp;nbsp;x(g) = g-f;   % Prepare x for doing cumsum (changes to numerical)&lt;br&gt;
&amp;nbsp;x = cumsum(x(1:end-1));   % Rank orderings&lt;br&gt;
&amp;nbsp;prctile_rank = floor(100*(x-1)/(length(x)-1))/100;   % Convert to percentile&lt;br&gt;
&amp;nbsp;prctile_rank(p) = prctile_rank;   % Restore the original order&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Apparently for repeated data values Excel uses the lowest percentile figure among them and this is what the above manipulation on x accomplishes.  This is the hard part of the needed computation.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Also it looks as though Excel uses truncation in arriving at percentiles values, which is why I have used the 'floor' function here in the next to last step.  You may prefer to alter this step if Excel actually uses more than two decimal places.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;As I have said, this is only a guess.  It would require many examples to be sure of the details in Excel's logic, or preferably some adequate documentation.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Fri, 17 Sep 2010 13:51:14 +0000</pubDate>
      <title>Re: replicating MS Excel percentrank formula</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/291712#780275</link>
      <author>Ashish Uthama</author>
      <description>Naresh,&lt;br&gt;
&lt;br&gt;
Dividing by the max assumes that your data starts at 1. Maybe you need &lt;br&gt;
to use the range?&lt;br&gt;
i.e if your data ranges from 5 - 50, the denominator should probably be:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;max(data(:,2) - min(data(:,2))&lt;br&gt;
&lt;br&gt;
The repeated element handling is also a concern (I do not know what the &lt;br&gt;
'right' answer for your application might be).&lt;br&gt;
&lt;br&gt;
Echoing Rogers comments:&lt;br&gt;
&amp;gt;  As I have said, this is only a guess.  It would require many examples &lt;br&gt;
&amp;gt; to be sure of the details in Excel's logic, or preferably some adequate &lt;br&gt;
&amp;gt; documentation.</description>
    </item>
    <item>
      <pubDate>Tue, 21 Sep 2010 13:20:05 +0000</pubDate>
      <title>replicating MS Excel percentrank formula</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/291712#781210</link>
      <author>Victor </author>
      <description>"Naresh Pai" &amp;lt;npai@uark.edu&amp;gt; wrote in message &amp;lt;i6tbnj$94h$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I am trying to replicate MS Excel's percentrank function in Matlab.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % example data&lt;br&gt;
&amp;gt; data =  [1.1 1.2 2.1 3.5 6.1 1.2 5.1 5.2 9.1 7.1]'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % excel percentrank results are:&lt;br&gt;
&amp;gt; % [0.00 0.11 0.33 0.44 0.77 0.11 0.55 0.66 1.00 0.88]'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Mathworks website suggests using the following formula to calculate percentile rank&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; prctile_rank = tiedrank(data)/length(data)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % and the result is:&lt;br&gt;
&amp;gt; prctile_rank = [0.10 0.25 0.40 0.50 0.80 0.25 0.60 0.70 1.00 0.90]'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Too bad. So, I try this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % clear previous variables&lt;br&gt;
&amp;gt; clear all&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % include serial number for sorting later&lt;br&gt;
&amp;gt; data(:,1) = 1:10;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % example data&lt;br&gt;
&amp;gt; data(:,2) = [1.1 1.2 2.1 3.5 6.1 1.2 5.1 5.2 9.1 7.1]';&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % sort the array using column 2&lt;br&gt;
&amp;gt; [x, i] = sort(data(:,2));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % store sorted array&lt;br&gt;
&amp;gt; data_sorted = data(i,:);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; %loop through&lt;br&gt;
&amp;gt; for i = 1:length(data)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     % find number of values less than&lt;br&gt;
&amp;gt;     less_than = numel(find(data_sorted(:,2)&amp;lt;data_sorted(i,2)));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     % percentile rank&lt;br&gt;
&amp;gt;     data_sorted(i,3) = floor((less_than)*10/length(data))/10;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     % clear temp variable&lt;br&gt;
&amp;gt;      clear less_than&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % put it back together&lt;br&gt;
&amp;gt; [y, j] = sort(data_sorted(:,1));&lt;br&gt;
&amp;gt; prctile_rank = data_sorted(j,3)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % the answer is:&lt;br&gt;
&amp;gt; prctile_rank = [0.00 0.10 0.30 0.40 0.70 0.10 0.50 0.60 0.90 0.80]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % close but not good enough. especially the last but one differs by 0.1&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Can somebody suggest a better way of doing this?&lt;br&gt;
&lt;br&gt;
Hi Naresh,&lt;br&gt;
&lt;br&gt;
if you replace&lt;br&gt;
&lt;br&gt;
&amp;gt;     % percentile rank&lt;br&gt;
&amp;gt;     data_sorted(i,3) = floor((less_than)*10/length(data))/10;&lt;br&gt;
&lt;br&gt;
with&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% percentile rank based on max value &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data_sorted(i,3) = less_than/(length(data)-1); &lt;br&gt;
&lt;br&gt;
you will get what you wanted. :)</description>
    </item>
    <item>
      <pubDate>Wed, 13 Oct 2010 18:11:04 +0000</pubDate>
      <title>Re: replicating MS Excel percentrank formula</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/291712#787502</link>
      <author>Skip Albertson</author>
      <description>I had written a function to do this and offer it here.  Depending on the data, you may need to sort it first and deal with replicates, otherwise this emulates the Excel PercentRank function fairly well:&lt;br&gt;
&lt;br&gt;
function PRank = PercentRank(x,p)&lt;br&gt;
%&lt;br&gt;
% function PRank = PercentRank(x,p);&lt;br&gt;
%&lt;br&gt;
% emulates the Percent Rank function found in Microsoft Excel.&lt;br&gt;
% Input at array x, and a value for which you would like to find&lt;br&gt;
% the percentile rank, p.  The value returned, PRank, will use&lt;br&gt;
% linery interpolation, as in Excel.  Extrapolation will&lt;br&gt;
% evaluate to NaN.&lt;br&gt;
%&lt;br&gt;
% For example:&lt;br&gt;
%&lt;br&gt;
% 	x=[5;&lt;br&gt;
% 	10;&lt;br&gt;
% 	15;&lt;br&gt;
% 	20;&lt;br&gt;
% 	95];&lt;br&gt;
%&lt;br&gt;
% 	p=15, returns 0.5, which is at the median&lt;br&gt;
%       p=14; returns 0.45, which is linearly interpolated.&lt;br&gt;
%&lt;br&gt;
% Skip Albertson, October 11, 2010&lt;br&gt;
%&lt;br&gt;
%&lt;br&gt;
y=zeros(size(x)); &lt;br&gt;
n=numel(x); &lt;br&gt;
for k=1:n &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r=numel(find(x&amp;lt;x(k))); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y(k)=r/(n-1); &lt;br&gt;
end&lt;br&gt;
PRank=interp1(x,y,p,'linear');&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Ashish Uthama &amp;lt;first.last@mathworks.com&amp;gt; wrote in message &amp;lt;i6vroi$lol$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Naresh,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Dividing by the max assumes that your data starts at 1. Maybe you need &lt;br&gt;
&amp;gt; to use the range?&lt;br&gt;
&amp;gt; i.e if your data ranges from 5 - 50, the denominator should probably be:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      max(data(:,2) - min(data(:,2))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The repeated element handling is also a concern (I do not know what the &lt;br&gt;
&amp;gt; 'right' answer for your application might be).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Echoing Rogers comments:&lt;br&gt;
&amp;gt; &amp;gt;  As I have said, this is only a guess.  It would require many examples &lt;br&gt;
&amp;gt; &amp;gt; to be sure of the details in Excel's logic, or preferably some adequate &lt;br&gt;
&amp;gt; &amp;gt; documentation.</description>
    </item>
    <item>
      <pubDate>Wed, 15 Dec 2010 20:56:05 +0000</pubDate>
      <title>replicating MS Excel percentrank formula</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/291712#805157</link>
      <author>Michael </author>
      <description>Hi, &lt;br&gt;
&lt;br&gt;
My collegue at work told me that if the percent rank of an input is  ALSO enumerated in the rank array, then an alternate formula in excel would be &lt;br&gt;
&lt;br&gt;
=rounddown(rank(target,list)-1/(count(list)-1),3) &lt;br&gt;
&lt;br&gt;
Some kludgie matlab code below : &lt;br&gt;
&lt;br&gt;
% minimal error checking so cuidado!  &lt;br&gt;
% include serial number for sorting later .. my data was 252 days of&lt;br&gt;
% rolling averages &lt;br&gt;
&lt;br&gt;
data(:,1) = 1:252; &lt;br&gt;
&lt;br&gt;
% example data - must be 252 rows .. can include zero .. I did not test for&lt;br&gt;
% negative numbers &lt;br&gt;
&lt;br&gt;
data(:,2) = Bri_252_rollArray; &lt;br&gt;
&lt;br&gt;
% sort the array using column 2 &lt;br&gt;
[x, i] = sort(data(:,2)); &lt;br&gt;
&lt;br&gt;
% store sorted array &lt;br&gt;
data_sorted = data(i,:); &lt;br&gt;
&lt;br&gt;
%loop through &lt;br&gt;
for i = 1:length(data) &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% find number of values less than target &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rank = numel(find(data_sorted(:,2)&amp;lt;data_sorted(i,2))); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% percentile rank &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x = rank/(length(data)-1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% rounddown a la excel .. there must be a better way in matlab but I am&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% a newbie hacker &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;z = num2str(x,4);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if length(z) &amp;gt;= 4&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;q = [z(1,1) z(1,2) z(1,3) z(1,4) z(1,5)];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;q = z(1,1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data_sorted(i,3) = str2double(q);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% clear temp variable &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;clear less_than &lt;br&gt;
end &lt;br&gt;
&lt;br&gt;
% put it back together &lt;br&gt;
[y, j] = sort(data_sorted(:,1)); &lt;br&gt;
prctile_rank = data_sorted(j,3); </description>
    </item>
    <item>
      <pubDate>Wed, 15 Dec 2010 22:42:05 +0000</pubDate>
      <title>replicating MS Excel percentrank formula</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/291712#805192</link>
      <author>Michael </author>
      <description>my bad ... &lt;br&gt;
&lt;br&gt;
=rounddown(rank(target,list)-1) / (count(list)-1),3) &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
"Michael " &amp;lt;m.revy@yahoo.com&amp;gt; wrote in message &amp;lt;ieba15$kbq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My collegue at work told me that if the percent rank of an input is  ALSO enumerated in the rank array, then an alternate formula in excel would be &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; =rounddown(rank(target,list)-1/(count(list)-1),3) &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Some kludgie matlab code below : &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % minimal error checking so cuidado!  &lt;br&gt;
&amp;gt; % include serial number for sorting later .. my data was 252 days of&lt;br&gt;
&amp;gt; % rolling averages &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; data(:,1) = 1:252; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % example data - must be 252 rows .. can include zero .. I did not test for&lt;br&gt;
&amp;gt; % negative numbers &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; data(:,2) = Bri_252_rollArray; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % sort the array using column 2 &lt;br&gt;
&amp;gt; [x, i] = sort(data(:,2)); &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % store sorted array &lt;br&gt;
&amp;gt; data_sorted = data(i,:); &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; %loop through &lt;br&gt;
&amp;gt; for i = 1:length(data) &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     % find number of values less than target &lt;br&gt;
&amp;gt;     rank = numel(find(data_sorted(:,2)&amp;lt;data_sorted(i,2))); &lt;br&gt;
&amp;gt;     &lt;br&gt;
&amp;gt;     % percentile rank &lt;br&gt;
&amp;gt;     x = rank/(length(data)-1);&lt;br&gt;
&amp;gt;     &lt;br&gt;
&amp;gt;     % rounddown a la excel .. there must be a better way in matlab but I am&lt;br&gt;
&amp;gt;     % a newbie hacker &lt;br&gt;
&amp;gt;     &lt;br&gt;
&amp;gt;     z = num2str(x,4);&lt;br&gt;
&amp;gt;     if length(z) &amp;gt;= 4&lt;br&gt;
&amp;gt;        q = [z(1,1) z(1,2) z(1,3) z(1,4) z(1,5)];&lt;br&gt;
&amp;gt;     else&lt;br&gt;
&amp;gt;         q = z(1,1);&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt;     data_sorted(i,3) = str2double(q);&lt;br&gt;
&amp;gt;     &lt;br&gt;
&amp;gt;     % clear temp variable &lt;br&gt;
&amp;gt;      clear less_than &lt;br&gt;
&amp;gt; end &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % put it back together &lt;br&gt;
&amp;gt; [y, j] = sort(data_sorted(:,1)); &lt;br&gt;
&amp;gt; prctile_rank = data_sorted(j,3); </description>
    </item>
  </channel>
</rss>
