I want to add array numbers in nested for loop.
inner loop depends on outer loop. It give me some unexpected results. Following is my code please check and guide me about that problem
inner loop depends on outer loop. It give me some unexpected results. Following is my code please check and guide me about that problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import numpy as np n = 5 A = np.zeros([n,n]) B = np.zeros([n]) sum = 0 for i in range (n): for j in range (n): A[i][j] = i - j for i in range ( 1 ,n): for j in range (i - 1 ): B[i] + = sum + A[i][j] print (B) |
Output:[0. 0. 2. 5. 9.]
I need the following resultOutput:[0, 1, 2, 5, 9]