Got Questions? Get Answers.
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:
2nd max is max of vec minus max(vec)

Subject: 2nd max is max of vec minus max(vec)

From: Dhruv Chhabra

Date: 29 Jun, 2012 20:51:12

Message: 1 of 10

2nd maximum is the max of the group when you remove the max from group.
lets say your vector is vec,
2nd maximum = max(vec(vec<max(vec)));

Subject: 2nd max is max of vec minus max(vec)

From: Star Strider

Date: 30 Jun, 2012 18:18:07

Message: 2 of 10

"Dhruv Chhabra" wrote in message <jsl4g0$3tv$1@newscl01ah.mathworks.com>...

--------------------------------------------------------------

I am confident you will find

>> doc sort

absolutely fascinating.

Subject: 2nd max is max of vec minus max(vec)

From: Jan Simon

Date: 30 Jun, 2012 20:11:23

Message: 3 of 10

Dear Dhruv Chhabra,

> 2nd maximum is the max of the group when you remove the max from group.
> 2nd maximum = max(vec(vec<max(vec)));

This is not efficient. Faster:

  [max1, index1] = max(vec);
  vec(index1) = -inf;
  [max2, index2] = max(vec);
  
Please note that the results differ, if the maximum value is not unique.
For a [1e6x1] random vector, this requires the half time.

Kind regards, Jan

Subject: 2nd max is max of vec minus max(vec)

From: Matt J

Date: 30 Jun, 2012 20:42:07

Message: 4 of 10

"Jan Simon" wrote in message <jsnmhb$755$1@newscl01ah.mathworks.com>...
> Dear Dhruv Chhabra,
>
> > 2nd maximum is the max of the group when you remove the max from group.
> > 2nd maximum = max(vec(vec<max(vec)));
>
> This is not efficient. Faster:
>
> [max1, index1] = max(vec);
> vec(index1) = -inf;
> [max2, index2] = max(vec);
>
> Please note that the results differ, if the maximum value is not unique.
> For a [1e6x1] random vector, this requires the half time.


I haven't tried it, but this is probably even more efficient:

http://www.mathworks.com/matlabcentral/fileexchange/23576-minmax-selection

Subject: 2nd max is max of vec minus max(vec)

From: Bruno Luong

Date: 1 Jul, 2012 07:43:08

Message: 5 of 10

"Matt J" wrote in message <jsnoav$cvb$1@newscl01ah.mathworks.com>...
>
> I haven't tried it, but this is probably even more efficient:
>
> http://www.mathworks.com/matlabcentral/fileexchange/23576-minmax-selection

As I understand, OP want the strict-next max value, this FEX won't do (sort() also don't do either, but unique() would do). Jan's suggestion is good.

Bruno

Subject: 2nd max is max of vec minus max(vec)

From: Greg Heath

Date: 1 Jul, 2012 12:07:06

Message: 6 of 10

"Jan Simon" wrote in message <jsnmhb$755$1@newscl01ah.mathworks.com>...
> Dear Dhruv Chhabra,
>
> > 2nd maximum is the max of the group when you remove the max from group.
> > 2nd maximum = max(vec(vec<max(vec)));
>
> This is not efficient. Faster:
>
> [max1, index1] = max(vec);
> vec(index1) = -inf;

vec(index) = []; % is about 10% faster

> [max2, index2] = max(vec);
>
> Please note that the results differ, if the maximum value is not unique.
> For a [1e6x1] random vector, this requires the half time.

Hope this helps.

Greg

Subject: 2nd max is max of vec minus max(vec)

From: Bruno Luong

Date: 1 Jul, 2012 12:19:09

Message: 7 of 10

"Greg Heath" <heath@alumni.brown.edu> wrote in message <jspeha$bmv$1@newscl01ah.mathworks.com>...

> >
> > [max1, index1] = max(vec);
> > vec(index1) = -inf;
>
> vec(index) = []; % is about 10% faster

It depends on the MATLAB you run.

On the more recent MATLAB, the first version vec(index1) = -inf does the filling inplace, and avoid moving memory around.

In the second way (deleting) MATLAB must create a new array and move the memory which is much costly.

For example in 2012a:

vec = ceil(100*rand(1,1e6));
[max1, index] = max(vec);

tic
vec(index) = -inf;
toc % Elapsed time is 0.000017 seconds.

tic
vec(index) = []; % is about 1000 times slower
toc % Elapsed time is 0.019579 seconds.

% Bruno

Subject: 2nd max is max of vec minus max(vec)

From: Matt J

Date: 1 Jul, 2012 13:54:07

Message: 8 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jsov2b$l0v$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <jsnoav$cvb$1@newscl01ah.mathworks.com>...
> >
> > I haven't tried it, but this is probably even more efficient:
> >
> > http://www.mathworks.com/matlabcentral/fileexchange/23576-minmax-selection
>
> As I understand, OP want the strict-next max value, this FEX won't do (sort() also don't do either, but unique() would do). Jan's suggestion is good.
>

Yes, but Jan's suggestion has the same problem when non-unique maxs are present.

Subject: 2nd max is max of vec minus max(vec)

From: Bruno Luong

Date: 1 Jul, 2012 14:36:07

Message: 9 of 10

"Matt J" wrote in message <jspkpv$2gv$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jsov2b$l0v$1@newscl01ah.mathworks.com>...
>
> Yes, but Jan's suggestion has the same problem when non-unique maxs are present.

Oh yeah, Jan's solution should be changed to

vec(vec==max1) = -inf;

as OP's request.

Bruno

Subject: 2nd max is max of vec minus max(vec)

From: Greg Heath

Date: 3 Jul, 2012 09:58:07

Message: 10 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jspf7t$dpb$1@newscl01ah.mathworks.com>...
> "Greg Heath" <heath@alumni.brown.edu> wrote in message <jspeha$bmv$1@newscl01ah.mathworks.com>...
>
> > >
> > > [max1, index1] = max(vec);
> > > vec(index1) = -inf;
> >
> > vec(index) = []; % is about 10% faster
>
> It depends on the MATLAB you run.
>
> On the more recent MATLAB, the first version vec(index1) = -inf does the filling inplace, and avoid moving memory around.
>
> In the second way (deleting) MATLAB must create a new array and move the memory which is much costly.
>
> For example in 2012a:
>
> vec = ceil(100*rand(1,1e6));
> [max1, index] = max(vec);
>
> tic
> vec(index) = -inf;
> toc % Elapsed time is 0.000017 seconds.
>
> tic
> vec(index) = []; % is about 1000 times slower
> toc % Elapsed time is 0.019579 seconds.

It also depends on how you time the process. Below I get a consistent (5 runs each)
factor of two

> ver

MATLAB Version 7.13.0.564 (R2011b)
Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1)
Java VM Version: Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM)
Client VM mixed mode

> clear all, clc
vec0 = rand(1,1e3);
tic
   for k =1:1e3
                  vec = vec0;
       [max1, index1] = max(vec);
            %vec(index1) = - inf;
                vec(index1) = [];
      [max2, index2] = max(vec);
   end
toc % 0.033 sec for -inf vs 0.064 sec for []

Hope this helps.

Greg

Tags for this Thread

No tags are associated with 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