p= np.array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])
I have the above array and need to find the items that are divisible by 2 and 3 without converting it to a flat array using for-loops and then slicing/masking.
I was able to figure out the for-loops part and am running into some issues with the slicing/masking part.
# we know that 6, 12 and 18 are divisible by 2 and 3
# therefore we can use slicing to pull those numbers out of the array
print(p[1:2,0:1]) # slice array to return 6
print(p[2:3,1:2]) # slice array to return 12
print(p[3:4,2:3]) # slice array to return 18
m=np.ma.masked_where(((p[:, :]%2==0)&(p[:, :]%3==0)),p)
print(m)
mask=np.logical_and(p%2==0,p%3==0)
print(mask)
Is there a more efficient way of slicing the array to find 6, 12 and 18? Also, is there a way to make either of the two mask functions output just 6, 12, and 18? The first one shows the inverse of what I want while the other returns a Boolean answer.