Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python diagonal sum

# A simple Python3 program to find
# sum of diagonals
MAX = 100
 
def printDiagonalSums(mat, n):
 
    principal = 0
    secondary = 0
    for i in range(0, n):
        principal += mat[i][i]
        secondary += mat[i][n - i - 1]
         
    print("Principal Diagonal:", principal)
    print("Secondary Diagonal:", secondary)
 
# Driver code
a = [[ 1, 2, 3, 4 ],
     [ 5, 6, 7, 8 ],
     [ 1, 2, 3, 4 ],
     [ 5, 6, 7, 8 ]]
printDiagonalSums(a, 4)
 
# This code is contributed
# by ihritik
Comment

how to sum numpy matrix diagonal

np.trace(matrix)
Comment

sum of diagonal numpy


arr= np.arange(16)
arr= np.reshape(arr,(4,4))

a=np.diagonal(arr, 0,0,1)

result=sum(a)
print(result)
Comment

PREVIOUS NEXT
Code Example
Python :: mapping in python 
Python :: joining two lists in python using for loop 
Python :: Fun & learn with python turtle 
Python :: python rounding 
Python :: how to run a python package from command line 
Python :: lstm pytorch documentation 
Python :: add key value in each dictonary in the list 
Python :: initialize 2d array of zeros python 
Python :: sort pandas dataframe by specific column 
Python :: python matrix determinant without numpy 
Python :: python socket get client ip address 
Python :: how to join two tuples in python 
Python :: partition python 
Python :: python create empty list 
Python :: python pattern 
Python :: how to check if a string value is nan in python 
Python :: open chrome console in selenium 
Python :: how to check if a list is empty in python 
Python :: how to convert uppercase to lowercase and vice versa in python 
Python :: python http post file 
Python :: python 3.4 release date 
Python :: flask_jinja structure 
Python :: python schleife 
Python :: get more than one decimal in python 
Python :: Get git sha 
Python :: how to kill python process started by excel 
Python :: gnuplot sum over a column 
Python :: jupyter notebook morse code francais 
Python :: access dynamicall to name attribute python 
Shell :: add-apt-repository command not found 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =