Personal IDL library for M. Galloy

single page | use frames     summary     class     fields     routine details     file attributes


top source mg_n_smallest

result = mg_n_smallest(data, n [, /largest])

Finds the n smallest elements of a data array. This algorithm works fastest on uniformly distributed data. The worst case for it is a single smallest data element and all other elements with another value. This will be nearly equivalent to just sorting all the elements and choosing the first n elements.

Return value

index array


data in required type=numeric array

data array of any numeric type (except complex/dcomplex)

n in required type=integer

number of smallest elements to find


largest in optional type=boolean

set to find n largest elements


For example, to find the 3 smallest values of 10 random values, try:

IDL> r = randomu(seed, 10) IDL> print, r, format='(4F)' 0.6297589 0.7815896 0.2508559 0.7546844 0.1353382 0.1245834 0.8733745 0.0753110 0.8054136 0.9513228 IDL> ind = mg_n_smallest(r, 3) IDL> print, r[ind] 0.0753110 0.124583 0.135338

File attributes

Modification date: Wed Sep 14 10:30:13 2011
Lines: 74
Docformat: rst rst
Personal library of Michael Galloy
Contact me if you have enhancement requests or bug fixes.