0

I want to get updated value of Pw at each iteration. For first iteration, it takes minimum value of first row and subtracts from Pw1. For second iteration, I want it to take this updated Pw (not Pw1) and subtract the minimum of second row.

import numpy as np
Pe=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
Pw1=100
Pe_min=Pe.min(axis=1)

for i in range(4):
    Pw=np.zeros(4)
    Pw[i]=Pw1-Pe_min[i]
    print(Pw)

The current output is

99
95
91
87

The output I am seeking is

99
94
85
72

1 Answer 1

1

Basically, You should update the Pw1 value in the each iteration. So you can add

Pw1-=Pe_min[i]

Full Code;

import numpy as np
Pe=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
Pw1=100
Pe_min=Pe.min(axis=1)
for i in range(4):
    Pw=np.zeros(4)
    Pw[i]=Pw1-Pe_min[i]
    Pw1-=Pe_min[i]
    print(Pw)
Sign up to request clarification or add additional context in comments.

2 Comments

How do I access the updated values of Pw? For instance, I want to call 85. If I use Pw, I get the output: [ 0. , 0. , 0. , 72]
If you're asking after termination of loop, you can not directly access. Because you declared the array in the loop, this is an local variable and you cannot access out of loop. But you can declare the array before the loop and you can do all of this process same.

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.