Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help: Finding 2nd max value index

Subject: Help: Finding 2nd max value index

From: Dan

Date: 10 Jan, 2008 10:18:02

Message: 1 of 11

Hi guys,

I am pretty new here.

 This code helps me to find the max value index. [C,index] = max(sim_output16,[],2);

I like to ask, how do i find the next max value index.
What code should i type. is there any ?

Subject: Help: Finding 2nd max value index

From: Anh Huy Phan

Date: 10 Jan, 2008 12:24:02

Message: 2 of 11

Dan <dan_ang_25@yahoo.com> wrote in message
<16223830.1199960323818.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hi guys,
>
> I am pretty new here.
>
> This code helps me to find the max value index. [C,index] =
max(sim_output16,[],2);
>
> I like to ask, how do i find the next max value index.
> What code should i type. is there any ?


Remove the largest elements in your data or set them to the
minimum value in each row.
Then find maximum value again.

Or you can use unique function
   a = [3 5 7 6 7]
   ua = unique(a)
        3 5 6 7
   max2nd = ua(end-1)


However, if you want to find the second maximum in each row of
one matrix, the code looks like this:

  a = randint(3,10,[1 8]) % data
  n = 2; % order
  mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))


where the function maxnd is as following

     function y = maxnd(x,n)
          y = unique(x);
          y = y(end-n+1);

HTH

Anh Huy Phan
RIKEN - BSI

Subject: Help: Finding 2nd max value index

From: Dan

Date: 10 Jan, 2008 13:45:27

Message: 3 of 11

a = randint(3,10,[1 8]) % data
n = 2; % order
mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))

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

Subject: Help: Finding 2nd max value index

From: Ian Clarkson

Date: 10 Jan, 2008 14:19:02

Message: 4 of 11

Dan <dan_ang_25@yahoo.com> wrote in message
<16045218.1199972757574.JavaMail.jakarta@nitrogen.mathforum.org>...
> a = randint(3,10,[1 8]) % data
> n = 2; % order
> mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))
>
> 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

Dan, I suggest you use MATLAB's extensive 'help' command.
If you don't understand the blkproc function, just type
'help blkproc' at the prompt. In fact, before reading this
thread, I didn't understand it either, and now I can see
exactly how it works by using the help function.

In words, it goes through a matrix and finds the second
highest value in every row. The blkproc function call could
be replaced with a for loop going through every row to find
the second highest value, but I'm guessing it's more
efficient (and elegant) to take this approach.

Subject: Help: Finding 2nd max value index

From: Anh Huy Phan

Date: 10 Jan, 2008 14:26:02

Message: 5 of 11

Dan <dan_ang_25@yahoo.com> wrote in message
<16045218.1199972757574.JavaMail.jakarta@nitrogen.mathforum.org>...
> a = randint(3,10,[1 8]) % data
> n = 2; % order
> mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))
>
> 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


% generate a random maxtrix a: 3x8 in the range [1 8]
% for more detail : help randint
% if you don't have this function, try
% a = ceil(8*rand(3,10))

a = randint(3,10,[1 8])

% specify the maximum degree
n = 2;


% use blkproc function in IMAGE PROCESSING TOOLBOX
% for block processing
% for more detail : help blkproc

mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))

% if you don't have this function, try this code
%
% mnd = zeros(size(a,1),1);
% for r = 1: size(a,1)
% ua = unique(a(r,:));
% mnd(r) = ua(end-n+1);
% end


You can copy and run the above code in the command window.
And you also need to create the function file 'maxnd.m' if
using blkproc funtion.

Anh Huy Phan
RIKEN - BSI

 

Subject: Help: Finding 2nd max value index

From: Dan

Date: 10 Jan, 2008 15:15:16

Message: 6 of 11

Thks Huy & Clarkson alot. A bow to you all.

i have acheived and understand it. And spending time to solve my codes. I learnt something new today.


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.

Subject: Help: Finding 2nd max value index

From: Anh Huy Phan

Date: 10 Jan, 2008 15:35:09

Message: 7 of 11

Dan <dan_ang_25@yahoo.com> wrote in message
<2717819.1199978159764.JavaMail.jakarta@nitrogen.mathforum.org>...
> Thks Huy & Clarkson alot. A bow to you all.
>
> i have acheived and understand it. And spending time to
solve my codes. I learnt something new today.
>
>
> 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.


Suppose that you want to find the 2nd max value in the
column 1, 3 and 4

  arrayfun(@(i) maxnd(a(:,i),2),[1 3 4])


Anh Huy Phan
RIKEN - BSI

Subject: Help: Finding 2nd max value index

From: Dan

Date: 10 Jan, 2008 16:04:38

Message: 8 of 11

Sorry Huy, i think you mistaken my meaning.

What i want to know is at

For each row, which column of a matrix is the 2nd max value retrieve from.

Not finding which 2nd max value in a column. thanks

Subject: Help: Finding 2nd max value index

From: Anh Huy Phan

Date: 10 Jan, 2008 17:58:02

Message: 9 of 11


Dan, you replace the maxnd function by the new one

function y = maxnd(x,n)
[xu,ind] = unique(x);
y = [xu(end-n+1) ind(end-n+1)];


One example shows how to use this function

a = [4 5 3 4 7
     8 3 2 1 6
     7 3 4 5 3];
n = 2;
mnd = blkproc(a,[1 size(a,2)],@(x) maxnd(x,n))


mnd =

     5 2
     6 5
     5 4


The first column is the 2nd max values along the rows, and
the 2nd column is the column indices of the correspondent
elements.

Anh Huy Phan
RIKEN - BSI

Subject: Help: Finding 2nd max value index

From: Dan

Date: 11 Jan, 2008 03:43:54

Message: 10 of 11

:( ... i do not have maxnd function, is there any other way out? Thks Huy. or anywwhere i can download this function

Subject: Help: Finding 2nd max value index

From: Anh Huy Phan

Date: 11 Jan, 2008 04:32:03

Message: 11 of 11

Dan <dan_ang_25@yahoo.com> wrote in message
<30744347.1200023064740.JavaMail.jakarta@nitrogen.mathforum.org>...
> :( ... i do not have maxnd function, is there any other
way out? Thks Huy. or anywwhere i can download this
function


Just copy and save the below code to a funtion file maxnd.m

function y = maxnd(x,n)
   [xu,ind] = unique(x);
   y = [xu(end-n+1) ind(end-n+1)];


Anh Huy Phan
RIKEN - BSI

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us