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

numpy get diagonal matrix from matrix

np.diag(np.diag(x))
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 :: float error python 
Python :: python using list as dictionary key 
Python :: looping through the list 
Python :: binary tree python implementation 
Python :: tkinter insert value box 
Python :: install tabula 
Python :: python selenium chrome save session 
Python :: concate the dataframe in pandas.. 
Python :: set pop in python 
Python :: hh:mm to mins in python 
Python :: not equal to in python 
Python :: python replace text 
Python :: how to get the end of a item in a python array 
Python :: python open file location 
Python :: remove list from list python 
Python :: how to move the element of an array in python by one step 
Python :: sum range 
Python :: python matplotlib pyplot set axis equals 
Python :: python create sqlite db file 
Python :: python cassandra 
Python :: select column in pandas dataframe 
Python :: rotate linked list 
Python :: pygame template 
Python :: change xlabel python 
Python :: stemmer nltk 
Python :: embed python discord 
Python :: pandas rearrange rows based on datetime index 
Python :: slicing in python list 
Python :: get table wikipedia 
Python :: block content 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =