Thread Subject: local maximum and minimum

Subject: local maximum and minimum

From: Baloff

Date: 4 Jun, 2005 04:14:05

Message: 1 of 11

Hello group

Is there a function that can help in doing the task below or do I need
to write something up?

I need to get the index of the relative “local” maximums and minimums in
a sequence or real numbers.

Say a={5,4,2,2,2,2,2,7}
The index I need is the one belong to the first occurrence of the number
2, which is the third number in the above sequence.

Say a={5,8,2,1}
The index I need is the one belongs to the number 8, which is the second
in the sequence.

Say a={5,4,2,2,2,7,2}
The index I need is the ones belong to the first 2 and then the 7. as an
index of local minimum and maximum respectively.

Thanks

Subject: local maximum and minimum

From: mst

Date: 3 Jun, 2005 13:59:39

Message: 2 of 11

Hi,
If I understand right, may be this is what you need to do.
first find max and min values of the matrix u have.

say this matrix has 6 values as follows :

X = [ 5 3 4 2 2 6]

have a loop:
for i = 1:6
   if (X(i) == minvalue)
        index = i;
        break;
   end
end

see if this works.



Baloff wrote:
> Hello group
>
> Is there a function that can help in doing the task below or do I need
> to write something up?
>
> I need to get the index of the relative "local" maximums and minimums in
> a sequence or real numbers.
>
> Say a={5,4,2,2,2,2,2,7}
> The index I need is the one belong to the first occurrence of the number
> 2, which is the third number in the above sequence.
>
> Say a={5,8,2,1}
> The index I need is the one belongs to the number 8, which is the second
> in the sequence.
>
> Say a={5,4,2,2,2,7,2}
> The index I need is the ones belong to the first 2 and then the 7. as an
> index of local minimum and maximum respectively.
>
> Thanks

Subject: local maximum and minimum

From: jerry

Date: 3 Jun, 2005 21:13:57

Message: 3 of 11

mst wrote:

> Hi,
> If I understand right, may be this is what you need to do.
> first find max and min values of the matrix u have.
>
> say this matrix has 6 values as follows :
>
> X = [ 5 3 4 2 2 6]
>
> have a loop:
> for i = 1:6
> if (X(i) == minvalue)
> index = i;
> break;
> end
> end
>
> see if this works.
>
>
X = [ 5 3 4 2 2 6];

find( min( X==min(X) ) )


--
Gerald Pollack
Dept. of Biology
McGill University

Subject: local maximum and minimum

From: jerry

Date: 3 Jun, 2005 21:16:07

Message: 4 of 11

jerry wrote:

> X = [ 5 3 4 2 2 6];
>
> find( min( X==min(X) ) )
oops ---
min( find( X==min(X) ) )
--
Gerald Pollack
Dept. of Biology
McGill University

Subject: local maximum and minimum

From: Paul Mennen

Date: 4 Jun, 2005 05:16:27

Message: 5 of 11

Baloff wrote:
> Is there a function that can help in doing the task below or do I need
> to write something up?
> I need to get the index of the relative “local” maximums and minimums in
> a sequence or real numbers.
> Say a={5,4,2,2,2,2,2,7}
> The index I need is the one belong to the first occurrence of the number
> 2, which is the third number in the above sequence.

There is no built-in function for that, however it shouldn't
be too hard to construct something to do the job. For instance
the following function will return the indicies of all local maxima
of the type you want:

find(a>=[a(2:end) inf] & a>[inf a(1:end-1)])

If the maxima is a sequence of several equal values, this will
return the index of the first of those. If you wanted it to
return the index of the last one, just swap the >= and the >.
If you wanted to include endpoints of the sequence as possible
local maxima, just change the two instances of "inf" to "-inf".

If you think about how this works, I think you will find it
easy to extend it to look for local minima as well.

~Paul

Subject: local maximum and minimum

From: Paul Mennen

Date: 4 Jun, 2005 05:23:48

Message: 6 of 11

jerry wrote:
> min( find( X==min(X) ) )

I don't think that is what he is looking for since
that finds only one local minima in the sequence.
I believe the poster wanted to find all the local minima
(and maxima) in the sequence. My previos response
accomplishes this (I believe).

~Paul

Subject: local maximum and minimum

From: Alex Nagel

Date: 4 Jun, 2005 05:58:05

Message: 7 of 11

Paul Mennen wrote:

>For instance the following function will return the indicies of all local maxima
>of the type you want:
>
>find(a>=[a(2:end) inf] & a>[inf a(1:end-1)])

try this on a = [1 2 2 3 2], it returns index 2 AND index 4. should be
only index 4, shouldn't it?

regards, alex

Subject: local maximum and minimum

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 4 Jun, 2005 19:17:29

Message: 8 of 11

In article <1117889884.959687.258850@g44g2000cwa.googlegroups.com>, "Alex
Nagel" <mail@alex-nagel.de> wrote:

> Paul Mennen wrote:
>
> >For instance the following function will return the indicies of all
local maxima
> >of the type you want:
> >
> >find(a>=[a(2:end) inf] & a>[inf a(1:end-1)])
>
> try this on a = [1 2 2 3 2], it returns index 2 AND index 4. should be
> only index 4, shouldn't it?
>
> regards, alex
-------
  Alex, I think Paul Mennen has given about the best answer possible,
given the inquiry. The OP used the word 'local', and in the examples
given, made it rather clear that a = [1 2 2 1 0] would be regarded as
having a maxima at index 2. To suppress this maxima at index 2 in your a
= [1 2 2 3 2] example because of a maxima occurring later at index 4 would
involve a non-local criterion, particularly if the string of repeating 2's
were greatly extended.

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford

Subject: local maximum and minimum

From: Baloff

Date: 5 Jun, 2005 07:50:30

Message: 9 of 11

Roger Stafford wrote:
> In article <1117889884.959687.258850@g44g2000cwa.googlegroups.com>, "Alex
> Nagel" <mail@alex-nagel.de> wrote:
>
>
>>Paul Mennen wrote:
>>
>>
>>>For instance the following function will return the indicies of all
>
> local maxima
>
>>>of the type you want:
>>>
>>>find(a>=[a(2:end) inf] & a>[inf a(1:end-1)])
>>
>>try this on a = [1 2 2 3 2], it returns index 2 AND index 4. should be
>>only index 4, shouldn't it?
>>
>>regards, alex
>
> -------
> Alex, I think Paul Mennen has given about the best answer possible,
> given the inquiry. The OP used the word 'local', and in the examples
> given, made it rather clear that a = [1 2 2 1 0] would be regarded as
> having a maxima at index 2. To suppress this maxima at index 2 in your a
> = [1 2 2 3 2] example because of a maxima occurring later at index 4 would
> involve a non-local criterion, particularly if the string of repeating 2's
> were greatly extended.
>
> (Remove "xyzzy" and ".invalid" to send me email.)
> Roger Stafford

to my understanding, if a = [1 2 2 3 2], then the local maxima "or at
least what I want" is the index of the number 3 since the first number 2
does not qualify.

thanks

Subject: local maximum and minimum

From: Paul Mennen

Date: 5 Jun, 2005 00:00:52

Message: 10 of 11

 > Baloff wrote:
 > to my understanding, if a = [1 2 2 3 2], then the local maxima "or at
 > least what I want" is the index of the number 3 since the first number 2
 > does not qualify.

You are absolutely corrrect. So my clever solution is not so
clever after all;

>> Roger Stafford wrote:
>> To suppress this maxima at index 2 in your a = [1 2 2 3 2] example
 >> because of a maxima occurring later at index 4 would involve a non-local
 >> criterion, particularly if the string of repeating 2's were greatly extended.

Yes, that does present a difficulty, but I think all is not lost.

Suppose we take this 15 element example:

a = [1 2 2 3 2 1 1 5 1 17 17 17 15 20 1 ];
% 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 - index of a

I think you will agree that Baloff will consider the local maxima
to occur at indicies: 4, 8, 10, 14

And that the local minima are at: 6, 9, 13

You will find that the following code produces this result exactly:

--------------------------------------------------------------
p = 1:length(a);
b = diff([inf a])~=0;
aa = a(b); pp = p(b);
maxima = pp(find(aa>[aa(2:end) inf] & aa>[inf aa(1:end-1)]))
minima = pp(find(aa<[aa(2:end) -inf] & aa<[-inf aa(1:end-1)]))
---------------------------------------------------------------

~Paul

Subject: local maximum and minimum

From: Gerald Pollack

Date: 6 Jun, 2005 13:33:49

Message: 11 of 11

Paul Mennen wrote:

> jerry wrote:
>> min( find( X==min(X) ) )
>
> I don't think that is what he is looking for since
> that finds only one local minima in the sequence.
> I believe the poster wanted to find all the local minima
> (and maxima) in the sequence. My previos response
> accomplishes this (I believe).
>
> ~Paul

Quoting the OP:
> Say a={5,4,2,2,2,2,2,7}
> The index I need is the one belong to the first occurrence of the number
> 2, which is the third number in the above sequence.

If all occurrences are needed, then leaving out the call to min() would do
that.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com