I define a Matrix NxN, with random values(0,1). I need to get the sum of the digits around the consecutive 1's.
For example:
100110001
101001000
100001001
000000000
000111001
000000100
.. ..
For 111 in the above, the sum of the surrounding digits is 1.
Is there some way using numpy or itertools or anything to get the sum or the array of the digits around?? Please help, cheers
For detecting random consecutives 1's i use:
from itertools import groupby
def groups(l):
return [sum(g) for i, g in groupby(l) if i == 1]
con += list(filter(lambda x: x > 1,groups(matrix[4])))
And to get the index of 1s :
idx+=[idx for idx, i in enumerate(matrix[4]) if i == 1]