I'm having a numpy ndarray where I would like to check if each row vector is monotonically increasing.
Example:
a = np.asarray([[1,2,3],[1,5,7],[4,3,6]])
monotonically_increasing(a)
Expected return:
[True, True, False]
I'm not entirely sure how to efficiently do this, since the matrices are expected to be quite large (~1000x1000), and was hoping for some help.
>>> import numpy as np
>>> a = np.asarray([[1,2,3],[1,5,7],[4,3,6]])
Find the difference between each element. np.diff
has an argument that lets you specify the axis to perform the diff
>>> np.diff(a)
array([[ 1, 1],
[ 4, 2],
[-1, 3]])
Check to see if each difference is greater than 0.
>>> np.diff(a) > 0
array([[ True, True],
[ True, True],
[False, True]], dtype=bool)
Check to see if all the differences are > 0
>>> np.all(np.diff(a) > 0)
False
>>>
As suggested by @Jaime - check that each element is greater than the element to its left:
np.all(a[:, 1:] >= a[:, :-1], axis=1)
Which appears to be about twice as fast/efficient as my diff solution.
You can make a function like this:
def monotonically_increasing(l):
return all(x < y for x, y in zip(l, l[1:]))
and then check for it, sublist for sublist, so
[monotonically_increasing(sublist) for sublist in a]