View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
extrema.m, extrema2.m

4.6 | 32 ratings Rate this file 172 Downloads (last 30 days) File Size: 51.6 KB File ID: #12275 Version: 1.0
image thumbnail

extrema.m, extrema2.m



14 Sep 2006 (Updated )

Search all extrema points of a time series or a surface with/without NaN's.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

Search all the maxima and minima points (peaks) in a time series (vector) or a surface (matrix), via columns, rows and crossing diagonals, ignoring NaN's elements.

[zmax,imax,zmin,imin]= extrema2(peaks);

where zmax (zmin) are the maxima (minima) elements and imax (imin) their linear index (check IND2SUB).

They come with the examples on the screenshot.

These are the same files as EXTREME.M and EXTREME2.M, but updated.


This file inspired Bi Dimensional Emperical Mode Decomposition (Bemd), Find Depressions, Telo Tool Terminal Restriction Fragment Analysis, Minima Maxima3 D, Extrema, Find Extrema, and Microscopy Image Browser (Mib).

MATLAB release MATLAB 7 (R14)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (49)
26 Dec 2016 HUI-CHUN HUANG

01 Dec 2016 Vic Hu

Vic Hu (view profile)


20 Nov 2016 Abhishek Dasgupta

where is the support for function handle?

21 Oct 2016 SL

SL (view profile)

It would be great to get an example how you apply `extreme2` on the plot `imagesc(x,y,C)`. - - I can get the frame so I.

14 Oct 2016 Stephanie Liu

excellent tool

05 Oct 2016 Christine Kakalou

05 Mar 2016 Saeed Asghari

05 Mar 2016 Saeed Asghari

26 Feb 2016 K G

K G (view profile)

I run the function

[hmax,imax,hmin,imin] = extrema2(h)

where h is a 1x15 vector (h=[11717 6172 2963 574 867 1099 1384 1726 2045 2230 2900 3460 3547 1625 617]) and I get the following error:

Attempted to access smaxcol(:,1); index out of bounds because
Error in extrema2 (line 73)
im = unique([smaxcol(:,1);smincol(:,1)]); % Rows with column extrema

Comment only
05 Feb 2016 Arun Kumar

Found it very useful. Thanks for this submission.

05 Jan 2016 Elsaeed Ali


20 May 2015 Ilya Belevich

06 May 2015 Mohamed

Mohamed (view profile)

This one should be added to Matlab native code ... :)

31 Mar 2015 Michael

Does the job of 'findpeaks' with less code, and can find minima with so much less hassle. Might as well be considered a 'required' add-on.

25 Nov 2014 Zohar Bar-Yehuda

Simple and useful.

16 Oct 2014 Phillip

Phillip (view profile)

Using this across a broad range of applications, very handy.

17 Sep 2014 Shuangshi

16 Sep 2014 sara

sara (view profile)

how can I use this mfile to run over my image?

Comment only
09 Jul 2014 karlosgk

karlosgk (view profile)

24 Mar 2014 Hugo

Hugo (view profile)

Thx, works great!

Comment only
11 Mar 2014 Vincent

thank you
this saves me a day of coding...

27 Jun 2013 Shirah

Shirah (view profile)

Hello, does anybody know how to replicate those detected extrema points onto a grayscale image.

Thank You

Comment only
10 Apr 2013 Rafael Freire

Do you have an option to put the sensitivity of the peaks? to only get the higher ones....

21 Mar 2013 Kumar

Kumar (view profile)

29 Jun 2012 M@lik Ali

Hi all i have to calculate the LUV color components any one can help me...
Thanks in Advance..

Comment only
30 Apr 2012 Camille Couzi

Hola Carlos!
Thanks for your contribution.
I am having some issues though... I am using your function inside a script which uses fminsearch also... and getting the following error message:

Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can
crash MATLAB and/or your computer.

Error in extrema

Error while evaluating uicontrol Callback

Help! How do I solve this problem!
Gracias y muchos saludos!

Comment only
22 Mar 2012 raj

raj (view profile)

{surf(T,f,G'), shading interp
[Gmax,imax,Gmin,imin] = extrema2(G);
hold on
for i = 1:length(Gmax)
text(x(imax(i)),y(imax(i)),zmax(i),[' ' num2str(zmax(i))])
hold off}
My problem is the T,f,G are of different sizes and an error comes up saying '??? Index exceeds matrix dimensions.' any suggestions please help

Comment only
22 Mar 2012 raj

raj (view profile)

I have a problem while using it for an array can u explain an example as u did it for a vector please, I am a begginer with this stuff

Comment only
04 Mar 2012 Luke Winslow

Luke Winslow (view profile)

04 Mar 2012 Luke Winslow

Luke Winslow (view profile)

14 Feb 2012 Anusha

Anusha (view profile)

Can someone help me on how to adapt this to detect maxima and minima points on an object edge?

Comment only
12 Dec 2011 Thomas

Thomas (view profile)

much better than findpeaks for my data thanks so much!

30 Oct 2011 Onkar Singh Gujral

25 Aug 2011 Aslak Grinsted

Aslak Grinsted (view profile)

Nice. There is also the built-in function findpeaks from the signal processing toolbox:

Comment only
29 Apr 2011 Rafael Freire

Perfect for finds maximun and minimuns in a surface! Great Job

15 Feb 2011 Rick Towler

Nice work.

Comment only
15 Feb 2011 Rick Towler

26 Mar 2010 Sanjeev

Excellent work

28 May 2009 joe saucer

hey guys.. is their anyway for this program to just give me the biggest maxes? my plot has many, many small peaks.. also, can you store these as variables?

Comment only
14 Nov 2008 Yin Wang

Excellent work, this is exactly what I need.

12 Jul 2008 angeles sierra

kiero agregar un linck a mi fotolog
pero no se como aserle

Comment only
09 Apr 2007 Carlos Adrián Vargas Aguilera

You are so right Dimitri! I've made a terrible translation from spanish, I meant "extrema points"... Ouch! I need to change even the name!

Comment only
06 Apr 2007 Dimitri Shvorob

It might be helpful to briefly explain the exact definition of 'extreme points'.

Comment only
01 Mar 2007 Jacob Eisenstein

thanks, very helpful.

15 Jan 2007 Johnny Job

Well done.

22 Nov 2006 Carlos Adrián Vargas Aguilera

Thanks JaeKyu Suhr!, new file with bug fixed in about 1 day...

Comment only
22 Nov 2006 JaeKyu Suhr

This is excellent, but when i use extreme2.m
There is an error related to a variable "indx".
Therefore, I put a little bit of code in "extreme.m" like this

% NaN's:
inan = find(isnan(x));
if ~isempty(inan)
indx = 1:Nt;
indx(inan) = [];
x(inan) = [];
Nt = length(x);
%%----added code
indx = 1:Nt;

14 Nov 2006 cloves souza

e muito legal

Comment only
15 Sep 2006 Sasha Trevizo

The author sure had a headache after he finished this, so, thanks for avoid it to me! It works really good!

13 Nov 2006

English translation from spanish. Little changes on the submit and screenshot.

17 Nov 2006

Now accepting NaN's.

22 Nov 2006

Fix bug in indx (by JaeKyu Suhr)

04 May 2016 1.0

BSD License

Contact us