Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create mask by first positions only

I have array:

a = np.array([[ 0,  1,  2,  0,  0,  0],
              [ 0,  4,  1, 35,  0, 10],
              [ 0,  0,  5,  4,  0,  4],
              [ 1,  2,  5,  4,  0,  4]])

I need select only from first consecutive 0 in each row:

[[  True   False  False  False  False  False]
 [  True   False  False  False  False  False]
 [  True   True   False  False  False  False]
 [  False  False  False  False  False  False]]

I try:

a[np.arange(len(a)), a.argmax(1): np.arange(len(a)), [0,0,0]] = True

But this is wrong.

like image 953
jezrael Avatar asked Sep 19 '17 14:09

jezrael


People also ask

How do you make an Opacity mask in Premiere?

Go to Effect Controls > Opacity > Mask. Here you'll find the option to add an elliptical mask or rectangular mask. Once you have selected one of the two, you will see the shape appear over your video.


1 Answers

You can use np.cumsum.

Assumption: you are looking for zeros only at the start of each row.

a = np.array([[ 0,  1,  2,  0,  0,  0],
              [ 0,  4,  1, 35,  0, 10],
              [ 0,  0,  5,  4,  0,  4]])

a.cumsum(axis=1) == 0
array([[ True, False, False, False, False, False],
       [ True, False, False, False, False, False],
       [ True,  True, False, False, False, False]], dtype=bool)

Basis: holds True for as long as the cumulative sum is 0 along each row.

Error-prone: an array with negative ints would cause this to fail. I.e. for [-1, 1], this would evaluate to True at position 1.

like image 140
Brad Solomon Avatar answered Sep 30 '22 11:09

Brad Solomon