Summing contiguous non zero tensor values
I am trying to find the summation of contiguous non zero tensor values as shown below

Let’s say, I have a tensor A = [1.3, 0.0, 0.6, 0.7, 0.8]. And I want to 

1) sum up the contiguous non-zero values of the tensor to output [1.3, 0.0, 2.1] and then choose the maximum which is 2.1.

2) find the indices as well which were used to sum these values. In this case it will be 2, 3, 4.

My approach is somewhat different from @Anwarvic. I try to do it in one pass. See the function below. It moves through the array and keep a log of max it has seen so far and current sum. Current sum is updated to 0 if we hit a zero or sum up the value to current if non-zero.

def find_continguous_max_sum(t): 
    max_,cur=0,0 
    max_indices, cur_indices = [], []

    for idx, i in enumerate(t): 
        if (i==0.0): 
            cur = 0 
            cur_indices = []
        else: 
            cur = cur+i
            cur_indices.append(idx)
        if (max_ &lt; cur): 
            max_ = cur 
            max_indices = cur_indices 

    return max_, max_indices

#usage
find_contiguous_max_sum(A)

