0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Is this equivalent to k[-1,0] < k[0:, 0]?
You can read about diff @user177324 Your formulation seems not match your title.
I understand the logic but k[-1,0] < k[0:, 0] produces all rows? Your approach if i understand it correctly assumes that the last row is always false?
My method would not produce a result for the last row. If you want to define the result of the last row as false, you can add it later. k[-1,0] only selects one element.
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.