how i can calculate the adjacent and opposite of this matrix?

Question:

i have this matrix

Matrix = [[0., 15., 19., 18., 17.],
     [15., 0., 14., 12., 23.],
     [19., 14.,  0., 14., 21.],
     [18., 12., 14., 0., 14.],
     [17., 23., 21., 14.,  0.]]

and i cut it to this by this code

c = [item[:index+1] for index, item in enumerate(Matrix[1:])]
c

it show that

[array([15.]),
 array([19., 14.]),
 array([18., 12., 14.]),
 array([17., 23., 21., 14.])]

and i want to calculate the adjacent and the opposite of this matrix example:
15+19+18+17 and 19+14+12+23 and 18+12+14+21 and 17 23 21 14
How i can do that

i tried this to append all data and work on it but he show nothing

arr=[]
for item,data in enumerate(c):
          for item2,data2 in enumerate(data):
                    arr.append(data2)
                    print(item,item2,data2)

print(arr)

**and i try the code below and he print the diagonal **

for item,data in enumerate(c):
          print(data[item:item+1])

and i try this and it show

for item,data in enumerate(c):
    print(item, data[0:1], data[1:2],data[2:3],data[3:4])
0 [15.] [] [] []
1 [19.] [14.] [] []
2 [18.] [12.] [14.] []
3 [17.] [23.] [21.] [14.]

so i realized that i can iterate just on columns not on row is there are a method method to solve it?
This is example that i want to implement to better understand, here i want to calculate the summation of these vector
this example that i want to implement

Asked By: Hi Chem

||

Answers:

It looks like you are trying to calculate sum of rows of ‘symmetrised’ submatrix. I’ll explain step by step.

At first, create a copy of submatrix:

>>> arr = Matrix[1:,:-1].copy()
>>> arr
array([[15.,  0., 14., 12.],
       [19., 14.,  0., 14.],
       [18., 12., 14.,  0.],
       [17., 23., 21., 14.]])

Then find a way to fill right upper triangle with values of left lower triangle. You could find both indices of left lower triangle and indices right upper triangle and use advanced indexing to modify values of arr:

>>> idx = np.triu_indices(len(arr), 1) #indices of right upper triangle
>>> arr[idx] = arr.T[idx] #use advanced indexing to modify values
>>> arr
array([[15., 19., 18., 17.],
       [19., 14., 12., 23.],
       [18., 12., 14., 21.],
       [17., 23., 21., 14.]])

For better understanding of indexing you could also try to take a look at indices like you did in your double for looping:

>>> np.transpose(idx)
array([[0, 1],
       [0, 2],
       [0, 3],
       [1, 2],
       [1, 3],
       [2, 3]])

Finally, calculate sums of rows:

>>> arr.sum(axis=0)
array([69., 68., 65., 75.]) #15+19+18+17, 19+14+12+23, 18+12+14+21 and 17+23+21+14 
Answered By: mathfux