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:
local min/max

Subject: local min/max

From: tfogg@email.unc.edu

Date: 28 Mar, 2009 00:20:13

Message: 1 of 5

I'm trying to right 2 functions that find the local mins and maxes
(valleys and peaks) of a matrix. The coordinates of the peaks (or
valleys) need to be displayed in a matrix where the first column is
the row numbers of the peaks and the second column is the column
numbers of the peaks. Does anybody have any suggestions as to how to
go about this? Here is the 10x10 test matrix I am using:

    0.0001 0.0013 0.0053 -0.0299 -0.1809 -0.2465
-0.1100 -0.0168 -0.0008 -0.0000
    0.0005 0.0089 0.0259 -0.3673 -1.8670 -2.4736
-1.0866 -0.1602 -0.0067 0.0000
    0.0004 0.0214 0.1739 -0.3147 -4.0919 -6.4101
-2.7589 -0.2779 0.0131 0.0020
   -0.0088 -0.0871 0.0364 1.8559 1.4995 -2.2171
-0.2729 0.8368 0.2016 0.0130
   -0.0308 -0.4313 -1.7334 -0.1148 3.0731 0.4444
2.6145 2.4410 0.4877 0.0301
   -0.0336 -0.4990 -2.3552 -2.1722 0.8856 -0.0531
2.6416 2.4064 0.4771 0.0294
   -0.0137 -0.1967 -0.8083 0.2289 3.3983 3.1955
2.4338 1.2129 0.2108 0.0125
   -0.0014 -0.0017 0.3189 2.7414 7.1622 7.1361
3.1242 0.6633 0.0674 0.0030
    0.0002 0.0104 0.1733 1.0852 2.6741 2.6725
1.1119 0.1973 0.0152 0.0005
    0.0000 0.0012 0.0183 0.1099 0.2684 0.2683
0.1107 0.0190 0.0014 0.0000

Subject: local min/max

From: Roger Stafford

Date: 28 Mar, 2009 02:33:02

Message: 2 of 5

tfogg@email.unc.edu wrote in message <9f1f8562-e900-4b49-a917-ed1e3a597421@r33g2000yqn.googlegroups.com>...
> I'm trying to right 2 functions that find the local mins and maxes
> (valleys and peaks) of a matrix. ........

  You haven't defined what you mean by the "local mins and maxes" in this context. How many neighboring values in the matrix must be less than a given value for it to be considered a peak value and where should they be located? Should it be the four nearest neighbors, the eight nearest, or some larger neighborhood. Also how do you wish the values on the edges and corners to be handled? There is no way to answer your question without important information like this.

  Furthermore, if you think about your problem carefully enough to be able to answer such questions, you might also discover how to find peaks and valleys by yourself without any help.

Roger Stafford

Subject: local min/max

From: Miroslav Balda

Date: 28 Mar, 2009 22:31:01

Message: 3 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gqk28u$1r1$1@fred.mathworks.com>...
> tfogg@email.unc.edu wrote in message <9f1f8562-e900-4b49-a917-ed1e3a597421@r33g2000yqn.googlegroups.com>...
> > I'm trying to right 2 functions that find the local mins and maxes
> > (valleys and peaks) of a matrix. ........
>
> You haven't defined what you mean by the "local mins and maxes" in this context. How many neighboring values in the matrix must be less than a given value for it to be considered a peak value and where should they be located? Should it be the four nearest neighbors, the eight nearest, or some larger neighborhood. Also how do you wish the values on the edges and corners to be handled? There is no way to answer your question without important information like this.
>
> Furthermore, if you think about your problem carefully enough to be able to answer such questions, you might also discover how to find peaks and valleys by yourself without any help.
>
> Roger Stafford

Hi,
There is a function extr in the FEX Id 10272. It find peaks and valeys of vectors. Maybe, you may adopt it for the use in matrices.
Good luck.
Mira

Subject: local min/max

From: aasim Azooz

Date: 29 Mar, 2009 00:56:01

Message: 4 of 5

tfogg@email.unc.edu wrote in message <9f1f8562-e900-4b49-a917-ed1e3a597421@r33g2000yqn.googlegroups.com>...
> I'm trying to right 2 functions that find the local mins and maxes
> (valleys and peaks) of a matrix. The coordinates of the peaks (or
> valleys) need to be displayed in a matrix where the first column is
> the row numbers of the peaks and the second column is the column
> numbers of the peaks. Does anybody have any suggestions as to how to
> go about this? Here is the 10x10 test matrix I am using:
>
> 0.0001 0.0013 0.0053 -0.0299 -0.1809 -0.2465
> -0.1100 -0.0168 -0.0008 -0.0000
> 0.0005 0.0089 0.0259 -0.3673 -1.8670 -2.4736
> -1.0866 -0.1602 -0.0067 0.0000
> 0.0004 0.0214 0.1739 -0.3147 -4.0919 -6.4101
> -2.7589 -0.2779 0.0131 0.0020
> -0.0088 -0.0871 0.0364 1.8559 1.4995 -2.2171
> -0.2729 0.8368 0.2016 0.0130
> -0.0308 -0.4313 -1.7334 -0.1148 3.0731 0.4444
> 2.6145 2.4410 0.4877 0.0301
> -0.0336 -0.4990 -2.3552 -2.1722 0.8856 -0.0531
> 2.6416 2.4064 0.4771 0.0294
> -0.0137 -0.1967 -0.8083 0.2289 3.3983 3.1955
> 2.4338 1.2129 0.2108 0.0125
> -0.0014 -0.0017 0.3189 2.7 7.1622 7.1361

You can use my M-files maxima and minima on the file exchange library. They work well for one dimentional arrays. you can modify them for a matrix that should be easy
Aasim Azooz
> 3.1242 0.6633 0.0674 0.0030
> 0.0002 0.0104 0.1733 1.0852 2.6741 2.6725
> 1.1119 0.1973 0.0152 0.0005
> 0.0000 0.0012 0.0183 0.1099 0.2684 0.2683
> 0.1107 0.0190 0.0014 0.0000

Subject: local min/max

From: Image Analyst

Date: 29 Mar, 2009 01:17:01

Message: 5 of 5

tfogg @ e mail. unc. edu :
If you have the image processing toolbox, this will find the two local peaks in your image. It just finds the very tip top of the peak, it does not try to spread out and grab part of the base of the peak, like going out so far as to where it starts to go up, level off, or hit the edge of the image (a much harder problem).

clc;
close all;
img = [0.0001 0.0013 0.0053 -0.0299 -0.1809 -0.2465 -0.1100 -0.0168 -0.0008 -0.0000;...
    0.0005 0.0089 0.0259 -0.3673 -1.8670 -2.4736 -1.0866 -0.1602 -0.0067 0.0000;...
    0.0004 0.0214 0.1739 -0.3147 -4.0919 -6.4101 -2.7589 -0.2779 0.0131 0.0020;...
   -0.0088 -0.0871 0.0364 1.8559 1.4995 -2.2171 -0.2729 0.8368 0.2016 0.0130;...
   -0.0308 -0.4313 -1.7334 -0.1148 3.0731 0.4444 2.6145 2.4410 0.4877 0.0301;...
   -0.0336 -0.4990 -2.3552 -2.1722 0.8856 -0.0531 2.6416 2.4064 0.4771 0.0294;...
   -0.0137 -0.1967 -0.8083 0.2289 3.3983 3.1955 2.4338 1.2129 0.2108 0.0125;...
   -0.0014 -0.0017 0.3189 2.7414 7.1622 7.1361 3.1242 0.6633 0.0674 0.0030;...
    0.0002 0.0104 0.1733 1.0852 2.6741 2.6725 1.1119 0.1973 0.0152 0.0005;...
    0.0000 0.0012 0.0183 0.1099 0.2684 0.2683 0.1107 0.0190 0.0014 0.0000]
subplot(1,3,1);
surf(img);
subplot(1,3,2);
imshow(img, []);
locaMaxima = imregionalmax(img)
subplot(1,3,3);
imshow(locaMaxima, []);

Look in the final image that prints to the command window. Whereever there is a 1, you have a pixel that is surrounded by lower valued pixels.

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