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:
Fast way to find all local minima

Subject: Fast way to find all local minima

From: Shaddy

Date: 30 Jan, 2009 18:45:04

Message: 1 of 5

All,

I'm looking for a very fast way find the true/false vector of all local minima for a vector of finite length

This is what I have so far:

for a column vector of real numbers, 'v'. The true/false vector storing locations of all local minima 'a' is

a=[false; (v(2:end-1)<v(3:end)) & (v(2:end-1)<v(1:end-2)); false];

Is there a faster way out there for large vectors v in Matlab?

Thanks,

Subject: Fast way to find all local minima

From: ImageAnalyst

Date: 30 Jan, 2009 19:37:15

Message: 2 of 5

On Jan 30, 1:45=A0pm, "Shaddy " <s.shokrallaREMOVET...@gmail.com> wrote:
> All,
>
> I'm looking for a very fast way find the true/false vector of all local m=
inima for a vector of finite length
>
> This is what I have so far:
>
> for a column vector of real numbers, 'v'. =A0The true/false vector storin=
g locations of all local minima 'a' is
>
> a=3D[false; (v(2:end-1)<v(3:end)) & (v(2:end-1)<v(1:end-2)); false];
>
> Is there a faster way out there for large vectors v in Matlab?
>
> Thanks,

---------------------------------------------------------------------------=
------------------------------
Shaddy:
Do you have the Image Processing Toolkit? You could use the erosion
function, which basically is a rank filter that returns a vector or
image that has at each pixel the minimum value of the original image
in a user defined window, such as a 3 by 3 window or a 5 by 5 window
or a 1 by 3 window (for vectors). The opposite, dilation, is a rank
filter on the max end (finds local maxima) and the median filter is a
rank filter than is half way up.
Regards,
ImageAnalyst

Subject: Fast way to find all local minima

From: Walter Roberson

Date: 30 Jan, 2009 20:03:09

Message: 3 of 5

Shaddy wrote:
 
> I'm looking for a very fast way find the true/false vector of all local minima for a vector of finite length

You could use diff(), and look for sign changes, perhaps via signum() and then
using a string search routine to look for (e.g.) [-1 1]. You might have to
add 1 to transform the [-1 0 1] of signum into a non-negative range,
You can explicitly char() the result if it makes you feel better about using
the string search routines, but the string search routines are happy to work
on numbers that can be represented as integers in the range 0 to 65535.

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: Fast way to find all local minima

From: someone

Date: 30 Jan, 2009 20:14:02

Message: 4 of 5

Walter Roberson <roberson@hushmail.com> wrote in message <E1Jgl.51382$aG1.41183@newsfe25.iad>...
> Shaddy wrote:
>
> > I'm looking for a very fast way find the true/false vector of all local minima for a vector of finite length
>
> You could use diff(), and look for sign changes, perhaps via signum() and then
> using a string search routine to look for (e.g.) [-1 1]. You might have to
> add 1 to transform the [-1 0 1] of signum into a non-negative range,
> You can explicitly char() the result if it makes you feel better about using
> the string search routines, but the string search routines are happy to work
> on numbers that can be represented as integers in the range 0 to 65535.
>

May not be the FASTEST, but "extrema" on the MATLAB FEX at:

http://www.mathworks.com/matlabcentral/fileexchange/12275

is very through and uses this technique.

> --
> .signature note: I am now avoiding replying to unclear or ambiguous postings.
> Please review questions before posting them. Be specific. Use examples of what you mean,
> of what you don't mean. Specify boundary conditions, and data classes and value
> relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: Fast way to find all local minima

From: Miroslav Balda

Date: 30 Jan, 2009 22:12:03

Message: 5 of 5

"Shaddy " <s.shokrallaREMOVETHIS@gmail.com> wrote in message <glvhrf$ikp$1@fred.mathworks.com>...
> All,
>
> I'm looking for a very fast way find the true/false vector of all local minima for a vector of finite length
>
> This is what I have so far:
>
> for a column vector of real numbers, 'v'. The true/false vector storing locations of all local minima 'a' is
>
> a=[false; (v(2:end-1)<v(3:end)) & (v(2:end-1)<v(1:end-2)); false];
>
> Is there a faster way out there for large vectors v in Matlab?
>
> Thanks,

Hi,
I have tested Your command on the vecror of 300000 samples of a random process and obtained 1843 minima in 0.034 secs. Since I had some problems with the command of similar structure many years ago, I built the function extr.m (FEX Id 10272), which found *all* real minima and maxima of the process without any problems. When I have used it now for the identical process, I have found 2645 minima in 0.059 seconds.
You see, it is not faster, however, it is reliable and fins all valeys (and peaks as well).
Good luck!

Mira

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