Skip to content
scipython.com

Q6.1.10Determining if an array is monotonically increasing

Write a one-line statement which returns True if an array is a monotonically increasing sequence or False otherwise.

Hint: numpy.diff returns the difference between consecutive elements of a sequence. For example,

In [x]: np.diff([1,2,3,3,2])
Out[x]: array([ 1,  1,  0, -1])
Show solution

The following statement will determine if a sequence a is increasing or not:

np.all(np.diff(a) > 0)

For example,

In [x]: np.all(np.diff([1,2,3,4,5]) > 0)
Out[x]: np.True_

In [x]: np.all(np.diff([1,2,3,4,4]) > 0)
Out[x]: np.False_