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:
Locating indices of more than one of the maximum values of an array

Subject: Locating indices of more than one of the maximum values of an array

From: Maya

Date: 3 Jul, 2010 09:13:04

Message: 1 of 8

Hey all,
i would really appreciate some help in a rather simple problem (i think is is anyway). I don't seem to be able to find a proper function for detecting the indices of multiple maxima in a (1 dimensional) array. That is, it would be really nice if the [C,I]=max(A,[],dim) function had an additional argument for providing a number of how many of the largest values you want to find. Because it's not necessary that you need only the index of the largest one, but maybe of the 2 largest values (the one being smaller than the other, yet larger than the rest) etc...
So, is there an easy way to do that? Thanks a lot! :)

Subject: Locating indices of more than one of the maximum values of an

From: darthshak

Date: 3 Jul, 2010 11:35:03

Message: 2 of 8

An immediate solution which comes to mind :

If you want to find the M largest values of vector A:
1) Sort the original array and store the result in B
2) Find the Mth largest value(which is in the (N-M+1)th location).
3) Using find() again, locate the indices of all elements greater than the above element.

Code:
B = sort(A);
smallest = B(N-M+1);
indices = find(A >= smallest);

Hope it helps. Do let me know if you have a more elegant way!

Subject: Locating indices of more than one of the maximum values of an array

From: us

Date: 3 Jul, 2010 11:56:02

Message: 3 of 8

"Maya " <fratz0la@hotmail.com> wrote in message <i0muv0$ram$1@fred.mathworks.com>...
> Hey all,
> i would really appreciate some help in a rather simple problem (i think is is anyway). I don't seem to be able to find a proper function for detecting the indices of multiple maxima in a (1 dimensional) array. That is, it would be really nice if the [C,I]=max(A,[],dim) function had an additional argument for providing a number of how many of the largest values you want to find. Because it's not necessary that you need only the index of the largest one, but maybe of the 2 largest values (the one being smaller than the other, yet larger than the rest) etc...
> So, is there an easy way to do that? Thanks a lot! :)

a hint:
- look at one of the many peak finders on the FEX...

us

Subject: Locating indices of more than one of the maximum values of an array

From: Roger Stafford

Date: 3 Jul, 2010 17:52:05

Message: 4 of 8

"Maya " <fratz0la@hotmail.com> wrote in message <i0muv0$ram$1@fred.mathworks.com>...
> Hey all,
> i would really appreciate some help in a rather simple problem (i think is is anyway). I don't seem to be able to find a proper function for detecting the indices of multiple maxima in a (1 dimensional) array. That is, it would be really nice if the [C,I]=max(A,[],dim) function had an additional argument for providing a number of how many of the largest values you want to find. Because it's not necessary that you need only the index of the largest one, but maybe of the 2 largest values (the one being smaller than the other, yet larger than the rest) etc...
> So, is there an easy way to do that? Thanks a lot! :)
- - - - - - - - - -
  As has been mentioned here, sorting is one way. Suppose you want the k largest values in A.

 [B,ix] = sort(A);
 B = B(1:k);
 ix = ix(1:k);

B and ix are the desired k largest values and the corresponding k indices, respectively.

  However, if k is small and A is long, this can be slower than need be. For example, if k is only 2, it really isn't necessary to sort the entire array just to find its two largest elements. One could do this:

 [b1,i1] = max(A);
 [b2,i2] = max(A([1:i1-1,i1+1:end]));
 if i2>=i1, i2 = i2+1; end
 B = [b1,b2]; % <-- The two largest elements of A
 ix = [i1,i2]; % <-- Their corresponding indices

Roger Stafford

Subject: Locating indices of more than one of the maximum values of an array

From: Roger Stafford

Date: 3 Jul, 2010 22:38:05

Message: 5 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i0ntc5$egm$1@fred.mathworks.com>...
> "Maya " <fratz0la@hotmail.com> wrote in message <i0muv0$ram$1@fred.mathworks.com>...
> > ....... That is, it would be really nice if the [C,I]=max(A,[],dim) function had an additional argument for providing a number of how many of the largest values you want to find. .......
> - - - - - - - - - -
> As has been mentioned here, sorting is one way. Suppose you want the k largest values in A.
> ........
- - - - - - - - -
  If you have a serious interest in efficient algorithms for larger values of the k referred to above, you might read the Wikipedia article "Selection Algorithm" at:

 http://en.wikipedia.org/wiki/Selection_algorithm

The section called "Selecting k smallest or largest elements" pertains to your problem and describes an algorithm that is only order n (for n elements in your A) if you do not need to have these k largest elements arranged in any particular order.

  It makes use of the "median of the medians" algorithm. As the article notes, "The resulting algorithm requires an expected time of only O(n), which is just about the best any algorithm can hope for."

Roger Stafford

Subject: Locating indices of more than one of the maximum values of an

From: ImageAnalyst

Date: 4 Jul, 2010 00:26:37

Message: 6 of 8

Maya:
You can find local maxima in 2D arrays using imregionalmax if you have
the Image Processing Toolbox. It's like a peak finder. From the
help:

"imregionalmax

Regional maxima
Syntax

BW = imregionalmax(I)
BW = imregionalmax(I,conn)
Description

BW = imregionalmax(I) finds the regional maxima of I. imregionalmax
returns the binary image BW that identifies the locations of the
regional maxima in I. BW is the same size as I. In BW, pixels that are
set to 1 identify regional maxima; all other pixels are set to 0.

Regional maxima are connected components of pixels with a constant
intensity value, and whose external boundary pixels all have a lower
value.

By default, imregionalmax uses 8-connected neighborhoods for 2-D
images and 26-connected neighborhoods for 3-D images. For higher
dimensions, imregionalmax uses conndef(ndims(I),'maximal').

BW = imregionalmax(I,conn) computes the regional maxima of I using the
specified connectivity. conn can have any of the following scalar
values."

You could then call bwlabel on the array returned from imregionalmax
to get a count of how many regional max it found.
-Image Analyst

Subject: Locating indices of more than one of the maximum values of an array

From: Maya

Date: 4 Jul, 2010 06:40:24

Message: 7 of 8

Thanks a lot all of you, those were quite interesting approaches you gave me :D
(needless to say, problem solved in many ways off course...)

Subject: Locating indices of more than one of the maximum values of an array

From: Bruno Luong

Date: 4 Jul, 2010 12:16:08

Message: 8 of 8

There is a MEX implementation of the selection algorithm with best complexity mentioned by James here:

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

Bruno

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