20

Is there a numpy function that gives for a given numpy array its maximum - minimum value, i.e. numpy.max(a) - numpy.min(a) ?

e.g.

numpy.xxx([4,3,2, 6] = 4 since max = 6, min = 2, 6 - 4 = 2)

Reason: performance increase since max and min would cause twice the iteration of the array (which is in my case 7.5 million or more numbers).

Robert Pollak
  • 3,751
  • 4
  • 30
  • 54
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119

1 Answers1

37

Indeed there is such a function -- it's called numpy.ptp() for "peak to peak".

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 8
    +1 … numpy should extend their function names to 30 characters. Answering here would be much faster… – eumiro Feb 16 '12 at 14:14
  • Thanks ... I really couldn't find it but I was close (looking at peaktopeak and others). – Michel Keijzers Feb 16 '12 at 14:15
  • 6
    Internally the `ptp` function computes the minimum and maximum separately, so this solution still iterates over the array twice. See [the ptp source code](https://github.com/numpy/numpy/blob/e7fe68ad3ce7f91cb152df49115f353ba9d75789/numpy/core/src/multiarray/calculation.c#L308) for details. – jochen Jul 22 '14 at 16:23