<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828</link>
    <title>MATLAB Central Newsreader - Help: Finding 2nd max value index</title>
    <description>Feed for thread: Help: Finding 2nd max value index</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 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.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 10 Jan 2008 10:18:02 -0500</pubDate>
      <title>Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408847</link>
      <author>Dan</author>
      <description>Hi guys, &lt;br&gt;
&lt;br&gt;
I am pretty new here.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;This code helps me to find the max value index. [C,index] = max(sim_output16,[],2);&lt;br&gt;
&lt;br&gt;
I like to ask, how do i find the next max value index.&lt;br&gt;
What code should i type. is there any ?</description>
    </item>
    <item>
      <pubDate>Thu, 10 Jan 2008 12:24:02 -0500</pubDate>
      <title>Re: Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408860</link>
      <author>Anh Huy Phan</author>
      <description>Dan &amp;lt;dan_ang_25@yahoo.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;16223830.1199960323818.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; Hi guys, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I am pretty new here.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  This code helps me to find the max value index. [C,index] = &lt;br&gt;
max(sim_output16,[],2);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I like to ask, how do i find the next max value index.&lt;br&gt;
&amp;gt; What code should i type. is there any ?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Remove the largest elements in your data or set them to the &lt;br&gt;
minimum value in each row.&lt;br&gt;
Then find maximum value again.&lt;br&gt;
&lt;br&gt;
Or you can use unique function&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;a = [3 5 7 6 7]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;ua = unique(a)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3 5 6 7&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;max2nd = ua(end-1)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
However, if you want to find the second maximum in each row of &lt;br&gt;
one matrix, the code looks like this:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;a = randint(3,10,[1 8])     % data&lt;br&gt;
&amp;nbsp;&amp;nbsp;n = 2; % order&lt;br&gt;
&amp;nbsp;&amp;nbsp;mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
where the function maxnd is as following&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;function y = maxnd(x,n)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y = unique(x);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y = y(end-n+1);&lt;br&gt;
&lt;br&gt;
HTH&lt;br&gt;
&lt;br&gt;
Anh Huy Phan&lt;br&gt;
RIKEN - BSI</description>
    </item>
    <item>
      <pubDate>Thu, 10 Jan 2008 13:45:27 -0500</pubDate>
      <title>Re: Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408868</link>
      <author>Dan</author>
      <description>a = randint(3,10,[1 8]) % data&lt;br&gt;
n = 2; % order&lt;br&gt;
mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))&lt;br&gt;
&lt;br&gt;
Sorry, i do not understand this code. how should i decipher it. sorry i am very new to matlab. i am still at the learning stage. can u explain to me</description>
    </item>
    <item>
      <pubDate>Thu, 10 Jan 2008 14:19:02 -0500</pubDate>
      <title>Re: Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408870</link>
      <author>Ian Clarkson</author>
      <description>Dan &amp;lt;dan_ang_25@yahoo.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;16045218.1199972757574.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; a = randint(3,10,[1 8]) % data&lt;br&gt;
&amp;gt; n = 2; % order&lt;br&gt;
&amp;gt; mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Sorry, i do not understand this code. how should i &lt;br&gt;
decipher it. sorry i am very new to matlab. i am still at &lt;br&gt;
the learning stage. can u explain to me&lt;br&gt;
&lt;br&gt;
Dan, I suggest you use MATLAB's extensive 'help' command. &lt;br&gt;
If you don't understand the blkproc function, just type &lt;br&gt;
'help blkproc' at the prompt. In fact, before reading this &lt;br&gt;
thread, I didn't understand it either, and now I can see &lt;br&gt;
exactly how it works by using the help function.&lt;br&gt;
&lt;br&gt;
In words, it goes through a matrix and finds the second &lt;br&gt;
highest value in every row. The blkproc function call could &lt;br&gt;
be replaced with a for loop going through every row to find &lt;br&gt;
the second highest value, but I'm guessing it's more &lt;br&gt;
efficient (and elegant) to take this approach. </description>
    </item>
    <item>
      <pubDate>Thu, 10 Jan 2008 14:26:02 -0500</pubDate>
      <title>Re: Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408872</link>
      <author>Anh Huy Phan</author>
      <description>Dan &amp;lt;dan_ang_25@yahoo.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;16045218.1199972757574.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; a = randint(3,10,[1 8]) % data&lt;br&gt;
&amp;gt; n = 2; % order&lt;br&gt;
&amp;gt; mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Sorry, i do not understand this code. how should i &lt;br&gt;
decipher it. sorry i am very new to matlab. i am still at &lt;br&gt;
the learning stage. can u explain to me&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
% generate a random maxtrix a: 3x8 in the range [1 8]&lt;br&gt;
% for more detail :    help randint    &lt;br&gt;
% if you don't have this function, try &lt;br&gt;
% a = ceil(8*rand(3,10))&lt;br&gt;
&lt;br&gt;
a = randint(3,10,[1 8]) &lt;br&gt;
&lt;br&gt;
% specify the maximum degree&lt;br&gt;
n = 2; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
% use blkproc function in IMAGE PROCESSING TOOLBOX &lt;br&gt;
% for block processing&lt;br&gt;
% for more detail :  help blkproc&lt;br&gt;
&lt;br&gt;
mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))&lt;br&gt;
&lt;br&gt;
% if you don't have this function, try this code&lt;br&gt;
%&lt;br&gt;
% mnd = zeros(size(a,1),1);&lt;br&gt;
% for r = 1: size(a,1)&lt;br&gt;
%     ua = unique(a(r,:));&lt;br&gt;
%     mnd(r) = ua(end-n+1);&lt;br&gt;
% end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
You can copy and run the above code in the command window. &lt;br&gt;
And you also need to create the function file 'maxnd.m' if &lt;br&gt;
using blkproc funtion.&lt;br&gt;
&lt;br&gt;
Anh Huy Phan&lt;br&gt;
RIKEN - BSI&lt;br&gt;
&lt;br&gt;
&amp;nbsp;</description>
    </item>
    <item>
      <pubDate>Thu, 10 Jan 2008 15:15:16 -0500</pubDate>
      <title>Re: Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408884</link>
      <author>Dan</author>
      <description>Thks Huy &amp; Clarkson alot. A bow to you all. &lt;br&gt;
&lt;br&gt;
i have acheived and understand it. And spending time to solve my codes. I learnt something new today.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Anyway, one last question is there any ways i can identify the 2nd max value, which column is it taken from. Eg. its from column 1, 2 , 3 ,4 or 5.</description>
    </item>
    <item>
      <pubDate>Thu, 10 Jan 2008 15:35:09 -0500</pubDate>
      <title>Re: Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408887</link>
      <author>Anh Huy Phan</author>
      <description>Dan &amp;lt;dan_ang_25@yahoo.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;2717819.1199978159764.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; Thks Huy &amp; Clarkson alot. A bow to you all. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; i have acheived and understand it. And spending time to &lt;br&gt;
solve my codes. I learnt something new today.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Anyway, one last question is there any ways i can &lt;br&gt;
identify the 2nd max value, which column is it taken from. &lt;br&gt;
Eg. its from column 1, 2 , 3 ,4 or 5.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Suppose that you want to find the 2nd max value in the &lt;br&gt;
column 1, 3 and 4 &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;arrayfun(@(i) maxnd(a(:,i),2),[1 3 4])&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Anh Huy Phan&lt;br&gt;
RIKEN - BSI</description>
    </item>
    <item>
      <pubDate>Thu, 10 Jan 2008 16:04:38 -0500</pubDate>
      <title>Re: Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408894</link>
      <author>Dan</author>
      <description>Sorry Huy, i think you mistaken my meaning.&lt;br&gt;
&lt;br&gt;
What i want to know is at &lt;br&gt;
&lt;br&gt;
For each row, which column of a matrix is the 2nd max value retrieve from. &lt;br&gt;
&lt;br&gt;
Not finding which 2nd max value in a column. thanks</description>
    </item>
    <item>
      <pubDate>Thu, 10 Jan 2008 17:58:02 -0500</pubDate>
      <title>Re: Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408909</link>
      <author>Anh Huy Phan</author>
      <description>&lt;br&gt;
Dan, you replace the maxnd function by the new one&lt;br&gt;
&lt;br&gt;
function y = maxnd(x,n)&lt;br&gt;
[xu,ind] = unique(x);&lt;br&gt;
y = [xu(end-n+1) ind(end-n+1)]; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
One example shows how to use this function&lt;br&gt;
&lt;br&gt;
a = [4     5     3     4     7&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;8     3     2     1     6&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;7     3     4     5     3];&lt;br&gt;
n = 2;&lt;br&gt;
mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
mnd =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5     2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;6     5&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5     4&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The first column is the 2nd max values along the rows, and &lt;br&gt;
the 2nd column is the column indices of the correspondent &lt;br&gt;
elements.&lt;br&gt;
&lt;br&gt;
Anh Huy Phan&lt;br&gt;
RIKEN - BSI</description>
    </item>
    <item>
      <pubDate>Fri, 11 Jan 2008 03:43:54 -0500</pubDate>
      <title>Re: Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408965</link>
      <author>Dan</author>
      <description>:( ... i do not have maxnd function, is there any other way out? Thks Huy. or anywwhere i can download this function</description>
    </item>
    <item>
      <pubDate>Fri, 11 Jan 2008 04:32:03 -0500</pubDate>
      <title>Re: Help: Finding 2nd max value index</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161828#408968</link>
      <author>Anh Huy Phan</author>
      <description>Dan &amp;lt;dan_ang_25@yahoo.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;30744347.1200023064740.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; :( ... i do not have maxnd function, is there any other &lt;br&gt;
way out? Thks Huy. or anywwhere i can download this &lt;br&gt;
function&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Just copy and save the below code to a funtion file maxnd.m&lt;br&gt;
&lt;br&gt;
function y = maxnd(x,n)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;[xu,ind] = unique(x);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;y = [xu(end-n+1) ind(end-n+1)];&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Anh Huy Phan&lt;br&gt;
RIKEN - BSI</description>
    </item>
  </channel>
</rss>

