3

The code down below goes through the Vals function and it assorts through the Numbers value and adds up all the sums after the sorting. I am trying to append the SUM values to the T_SUM to store the values of each SUM for each sort.

Vals= np.arange(start=60, stop=105, step=5)
Numbers = np.array([123.6,       130 ,       150,        110.3748,     111.6992976,
 102.3165566,   97.81462811 , 89.50038472 , 96.48141473 , 90.49956702, 65])
T_Sum = np.array([])
p= 0 

while len(Vals) != p:
    Numbers= np.where(Numbers >= Vals[p],Numbers ,0 )
    p = p + 1
    SUM = np.sum(Numbers)
    T_Sum = np.concatenate((T_Sum, SUM))

print(T_Sum)
1
  • So what's the question or problem? Commented Mar 3, 2021 at 3:37

2 Answers 2

2

You can use masked arrays to mask values you desire and then sum them up all in one line:

T_Sum = np.ma.masked_array(np.repeat(Numbers[None,:],Vals.size,0),mask=[Numbers<Vals[:,None]]).sum(-1).data

output:

array([1167.28664878, 1167.28664878, 1102.28664878, 1102.28664878,
       1102.28664878, 1102.28664878, 1012.78626406,  922.28669704,
        727.9906542 ])
Sign up to request clarification or add additional context in comments.

1 Comment

Hello there I made another issue for a tpic like this if you take a look at it I would appreciate it: stackoverflow.com/questions/66466529/…
1

This should do the same thing:

Vals= np.arange(start=60, stop=105, step=5)
Numbers = np.array([123.6,       130 ,       150,        110.3748,     111.6992976,
 102.3165566,   97.81462811 , 89.50038472 , 96.48141473 , 90.49956702, 65])
T_sum = np.empty(len(Vals))
count = 0

for i in Vals:
    Numbers= np.where(Numbers >= Vals[p],Numbers ,0 )
    SUM = np.sum(Numbers)
    T_sum[count] = SUM
    count += 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.