user8682794
user8682794

Reputation:

Check whether numpy array row is smaller than the next

Suppose i have the following array:

In [1]: k
Out[1]: 
array([[0],
       [1],
       [2],
       [7],
       [4],
       [5],
       [6]])

I would like to check whether each row of k is smaller than the next.

I tried the following but it did not work:

In [2]: k[:,0]<k[:+1,0]
Out[2]: array([False, False, False, False, False, False, False], dtype=bool)

What am i missing here?

Upvotes: 0

Views: 581

Answers (2)

Tai
Tai

Reputation: 7994

You can also use np.diff along axis 0 and check whether the result is greater than 0.

arr = np.array([[0],
                [1],
                [2],
                [7],
                [4],
                [5],
                [6]])
np.diff(arr, axis=0) > 0

array([[ True],  1 > 0
       [ True],  2 > 1
       [ True],  7 > 2
       [False],  4 > 7
       [ True],  5 > 4
       [ True]]) 6 > 5

There is no row follow [6] and thus the result is one row shorter.

Upvotes: 1

llllllllll
llllllllll

Reputation: 16404

The k[:+1,0] means "from 0 to +1", there is only one element. You need:

k[:-1, 0] < k[1:, 0] 

Upvotes: 1

Related Questions