Computing sum of consecutive values in a vector that are greater than a constant number?

sum of consecutive numbers in java
how to count consecutive numbers in java
sum of consecutive numbers in array
c# program to check consecutive numbers
given an integer total calculate the number of possible ways to represent total
matlab find consecutive repeated values
ways to sum - leetcode
count ways to express a number as sum of powers

I couldn't summarize my question in the title very well. I'm writing a code and in one part of the code I need to compute the following:

Let's say we have a vector (e.g. a numpy array):

a = [3.2, 4, 7, 2, 8, 9, 7, 1.7, 2, 8, 9, 1, 3]

We want to turn any number greater than 5 to 5:

a = [3.2, 4, 5, 2, 5, 5, 5, 1.7, 2, 5, 5, 1, 3]

Then we compute the sum of consecutive 5s and the number that follows them and replace all these elements with the resulting sum:

a = [3.2, 4, 5+ 2, 5+ 5+ 5+ 1.7, 2, 5+ 5+ 1, 3]

so the resulting array would be:

a = [3.2, 4, 7, 16.7, 2, 11, 3]

I can do this using a for loop like this:

    indx = np.where(a>5)[0]
    a[indx] = 5
    counter = 0
    c = []
    while (counter < len(a)):
        elem = a[counter]
        if elem ~= 5:
            c.append(elem)
        else:
            temp = 0
            while(elem==5):
                temp += elem
                counter +=1
                elem = a[counter]
            temp += elem
            c.append(temp)
        counter += 1

Is there a way to avoid using the for loop? Perhaps by using the indx variable?

I have a vague idea if we turn it into a string: a = '[3.2, 4, 5, 2, 5, 5, 5, 1.7, 2, 5, 5, 1, 3]' and then change anywhere we have ' 5,' with ' 5+' and then use eval(a). However, is there an efficient way to find all indices containing a sub-string? How about the fact that strings are immutable?

You can use pandas for data manipulation, using cumsum and shift to groupby your values with your logic, and aggregating it with sum

df = pd.DataFrame(a, columns=['col1'])
df.loc[df.col1 > 5] = 5
s = df.col1.groupby((df.col1 != 5).cumsum().shift().fillna(0)).sum()

col1
0.0     3.2
1.0     4.0
2.0     7.0
3.0    16.7
4.0     2.0
5.0    11.0
6.0     3.0

To get a numpy back, just get .values

>>> s.values
array([  3.2,   4. ,   7. ,  16.7,   2. ,  11. ,   3. ])

Print all possible consecutive numbers with sum N, One important fact is we can not find consecutive numbers above N/2 that adds up to N, because N/2 + (N/2 + 1) would be more than N. So we start from start = 1​  I have a vector of monthly values. I would now like to identify periods of months where the values exceeded my threshold for 5 months. I know I can find all the individual months with the find function but how do I add this extra condition saying, only show me their position if five months in a row exceeded the threshold?

This is what you want (all in vectorized numpy):

import numpy as np

a = np.array([0, 3.2, 4, 7, 2, 8, 9, 7, 1.7, 2, 8, 9, 1, 3, 0]) # add a 0 at the beginning and the end
aa = np.where(a>5, 5, a) # clip values to 5, can use np.clip(a, None, 5) too...
c = np.cumsum(aa) # get cumulative sum
np.diff(c[aa < 5]) # only keep values where original array is less than 5, then diff again

array([ 3.2,  4. ,  7. , 16.7,  2. , 11. ,  3. ])

Ways to sum to N using array elements with repetition allowed , Given a set of m distinct positive integers and a value 'N'. The problem is to count the total number of ways we can form 'N' by doing sum of the array elements. and 1s such that no consecutive elements are 1 · Number of ways to choose an exactly K elements greater than it in the given array · Number of ways to form an  Write a function called max_sum that takes v, a row vector of numbers, and n, a positive integer as inputs. The function needs to find the n consecutive elements of v whose sum is the largest possible.

How to count the number of consecutive numbers of the same value , Where the first value of 1 stays constant for another 2 steps, the second stays constant for one more step etc. 0 Comments. ShowHide all  S = sum(A,vecdim) sums the elements of A based on the dimensions specified in the vector vecdim. For example, if A is a matrix, then sum(A,[1 2]) is the sum of all elements in A , since every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

Find a series of consecutive numbers in a vector, in a vector. Learn more about consecutive. Hello, I have a small problem I am trying to solve on Matlab, but I am a stuck. I have a row vector. N = 5; % Required number of consecutive numbers following a first one MathWorks is the leading developer of mathematical computing software for engineers and scientists. Number of pairs in an array with the sum greater than 0; Minimum number of swaps required to sort an array of first N number; How to flatten a Vector of Vectors or 2D Vector in C++; Maximum GCD of all subarrays of length at least 2; Check if its possible to make sum of the array odd with given Operations; Check whether a Matrix is a Latin

Astronomical Papers Prepared for the Use of the American Ephemeris , The numbers from the first four tables are to be first taken out for several consecutive values of g, and the sum interpolated to the actual value the radius vector, found by entering Table XXIII with the Argument K. The sum We then take from Tables XXVI and XXVII the principal term of the latitude and its secular variation. Menna - rather than using find just use the greater than operator to determine which elements of form are greater than 0.8 and then sum the result. Note that. will return an array of 0s and 1s where each zero indicates that the element in form at the index where the zero appears is not greater than 0.8, and each one indicates that the element

Segment Tree, This includes finding the sum of consecutive array elements a[l…r], or finding the On the basis of these values, we can compute the sums of the previous level. The constant INF is equal to some large number that is bigger than all numbers in Instead of storing a vector or a multiset in each vertex, other data structures  We see that rle() returns a list of two elements: lengths and values, where the latter gives the unique number of each run, and the former gives the run length, i.e. the number of consecutive repeats within each run. For example, the first run is the number 2 repeated 5 times, and the second run is the number 5 repeated once.

Comments
  • in numpy you can use np.where(a>=5) to get the indices
  • Thank you! Can we also use this method if "a" is a 2D array and we want to compute this for individual rows and fillna with np.inf?
  • Your answer fails for, e.g. arr = [3.2, 4, 7, 2, 8, 9, 7, 1.7, 2, 8, 9, 1, 3,5,2,5,5,5,5,5,5,5,5,5,5,10] or if arr finishes with 5
  • Is this the only case that it fails? What if we also add a "0" at the end of the array?
  • I would use numpy.clip to clip.
  • Note that the final 0 will show if your last value was <5, so this might need an extra check if you need it gone... Or maybe there is a cleaner less hacky way I didn't think of...
  • Thanks but I'm not sure this is much different. I'm looking for a loop-free solution if it exists.
  • I think any solution will require at least one pass over the array. Even if you were only looking to accomplish the first task, converting any number greater than 5 to 5, you would still need to evaluate each item once.