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

matrix diagonal sum Python

# Problem Link : https://leetcode.com/problems/matrix-diagonal-sum/

class Solution(object):
    def diagonalSum(self, array):
        """
        :type array: List[List[int]]
        :rtype: int
        """
        n = len(array)
        primary = 0
        secondary = 0;
        for i in range(0, n):
            primary += array[i][i]
            secondary += array[i][n-i-1]
        if (n % 2 == 0): return primary + secondary
        else: return primary + secondary - array[n//2][n//2]
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 :: for one line python 
Python :: django python base 64 decode 
Python :: python round to two decimals 
Python :: python multiple inheritance 
Python :: menubar pyqt 
Python :: how to import your own function python 
Python :: how to get the first key of a dictionary in python 
Python :: pandas df to mongodb 
Python :: how to get the ip address of laptop with python 
Python :: random number generator in python 
Python :: convert rgb to a single value 
Python :: How to loop over grouped Pandas dataframe? 
Python :: python split every character in string 
Python :: how to get time in python 
Python :: Python how to compile to exe file 
Python :: Display if the column(s) contain duplicates in the DataFrame 
Python :: how to loop over list 
Python :: keras declare functional model 
Python :: python edit global variable in function 
Python :: print multiple lines python 
Python :: [0] * 10 python 
Python :: # extract an email ID from the text using regex 
Python :: How to efficiently calculate the nth Catalan number, in Python? 
Python :: import django value 
Python :: numpy average 
Python :: fillna method 
Python :: python list for all months including leap years 
Python :: how to concat on the basis of particular columns in pandas 
Python :: number of days in a month python 
Python :: discord python tts 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =