Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matrix Calculation Problem
#1
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

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 result
Output:
[0, 1, 2, 5, 9]
Reply
#2
Why do you expect the result to be [0, 1, 2, 5, 9]? What do you want to sum exactly?
Reply
#3
for i in range(1,n):
    for j in range(i-1):
        B[i] += sum + A[i][j]
In the first iteration of the loops i equals 1 therefore "j in range(0)" is not executed and B[1] is never updated.
Reply
#4
Why j in range(0) is not excuted because it value present in array A. i.e A[1][0]. why the code doesnot pick this value?

(Nov-04-2019, 09:20 AM)Gribouillis Wrote: Why do you expect the result to be [0, 1, 2, 5, 9]? What do you want to sum exactly?

I want to add the array elements row wise.
when i=1 then range of j is 0. so according to the code
B[1]+=A[1][0]
for i=2 the range for j is 1.
B[2]+=A[2][1]
for i=3, j=0, 1, 2
B[3]+=A[3][0]+A[3][1]+A[3][2]
Actually i want the above mentioned sum

the code give correct result for i=2, 3 and 4 but not for i=1.
i want the code execute for i=1 also

(Nov-04-2019, 09:07 AM)arshad Wrote: 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
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 result
Output:
[0, 1, 2, 5, 9]

ok sorry i will do this next time thanks
Reply
#5
(Nov-04-2019, 02:55 PM)arshad Wrote: Why j in range(0) is not excuted because it value present in array A. i.e A[1][0]. why the code doesnot pick this value?

range(0) returns a 0. That means that it will NOT iterate (or it will iterate 0 amount of times) and it goes directly to the next iteration on the upper loop where i=2.


This check outputs what you were asking for:
for i in range(n):
    if i <= 1:
        value=1
    else:
        value=i-1
    for j in range(value):
        B[i] += sum + A[i][j]
Output:
[0. 1. 2. 5. 9.]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 778 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Stock Return calculation problem LFin 10 1,946 Sep-26-2022, 04:28 PM
Last Post: deanhystad
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,300 May-03-2021, 06:30 AM
Last Post: Gribouillis
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,132 Aug-28-2020, 02:41 PM
Last Post: sparkt
  Problem in matrix leemao 1 2,115 Jun-27-2020, 12:48 PM
Last Post: Yoriz
  Problem with simple 3D Vektor calculation Pythocodras 0 1,679 Dec-11-2019, 07:18 PM
Last Post: Pythocodras
  Correlation Matrix Problem Pmllz 0 1,988 Feb-02-2019, 10:19 PM
Last Post: Pmllz
  matrix from matrix python numpy array shei7141 1 3,642 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020