


Python program to calculate the sum of the right diagonal elements of a matrix
A popular general-purpose programming language is Python. It is used in a variety of industries, including desktop applications, web development, and machine learning. Fortunately, Python has a simple and easy-to-understand syntax that is suitable for beginners. In this article, we will use Python to calculate the sum of the right diagonal of a matrix.
What is a matrix?
In mathematics, we use a rectangular array or matrix to describe a mathematical object or its properties. It is a rectangular array or table containing numbers, symbols, or expressions arranged in rows. and column arrangement.
For example −
2 3 4 5 1 2 3 6 7 5 7 4
So, this is a matrix with 3 rows and 4 columns, expressed as a 3*4 matrix.
Now, there are two diagonals in the matrix, the main diagonal and the sub-diagonal. The main diagonal is the diagonal line from the upper left corner to the lower right corner, and the secondary diagonal is the diagonal line from the lower left corner to the upper right corner.
We can see from the above example that a00 and a11 are both main diagonals (left diagonals), while a10 and a01 are secondary diagonals (right diagonals), as shown below
2 3 a<sub>00</sub> a<sub>01</sub> 1 2 a<sub>10</sub> a<sub>11</sub>
Sum of right diagonal of matrix
Since we have revised the basic concepts and have a complete understanding of matrices and diagonals, let us now delve deeper into the topic and complete the coding part of the concept.
To calculate the sum, we can use a 2D matrix. Consider a 4*4 matrix whose elements are
Here, a00, a11, a22 and a33 are the main diagonal elements of the matrix. The subdiagonal consists of elements a30, a21, a12 and a03.
Before completing this task, there is an important condition to consider: in order to take the sum of the elements on the main diagonal, it must satisfy the so-called row and column conditions, that is, each element in each row must have a Equal column numbers.
Similarly, for calculating the sum of elements on the sub-diagonal (a03, a12, a21 and a30), the row and column conditions will be equal to the number of rows minus the number of columns minus 1.
2 4 6 8 a00 a01 a02 a03 3 5 7 9 a10 a11 a12 a13 1 4 6 7 a20 a21 a22 a23 3 5 1 4 a30 a31 a32 a33
Use For Loop
In this method we will use two loops, one for rows and columns and another to check the condition we provide.
algorithm
Give a value, which is the maximum value.
Define a function for the matrix.
Use a for loop to iterate over numbers
Provide conditions for the right diagonal of the matrix.
Print this value.
Example
This example defines a constant MAX with a value of 50, and then creates a function called SUM_RIGHT_MATRIX that accepts a matrix and an integer as parameters.
This function adds all the numbers on the right diagonal of the given matrix (i.e. from the upper right corner to the lower left corner) and prints out the sum.
MAX = 50 def SUM_RIGHT_MATRIX (matrix, m): rightD = 0; for i in range (0, m): for j in range (0, m): if ((i + j) == (m - 1)): rightD += matrix[i][j] print ("Sum of right diagonal is:", rightD) T = [[ 13, 21, 33, 45 ], [ 52, 16, 27, 28 ], [ 17, 28, 31, 43 ], [ 54, 26, 87, 28 ]] SUM_RIGHT_MATRIX (T, 4)
Output
After executing the above program, we get "The sum of the right diagonals is: 155". This means the sum of all the numbers on the right diagonal is 155.
Sum of right diagonal is: 154
Use a single loop
Using this method, the sum of the main diagonal and the sub-diagonal is calculated through a loop.
algorithm
Give a value, which is the maximum value.
Define a function for the matrix.
Use a for loop to iterate over numbers.
Provide conditions for the right diagonal of the matrix.
Print this value.
Example
The following example defines a function named sumofrightdiagonal that accepts two parameters: a matrix and m.
It loops through the matrix and adds each number on the right diagonal of the matrix, storing them in a variable called right_diagonal.
Finally, it prints "Sum of Right Diagonal is:", followed by the value stored in right_diagonal. The example also includes an input example T (a 4x4 matrix) where m equals 4, so when Sumofrightdiagonal is called with these values as arguments, it will calculate and print out the sum of all elements on the right diagonal of T.
MAX = 50 def sumofrightdiagonal (matrix, m): right_diagonal = 0 for i in range (0, m): right_diagonal += matrix [i] [m - i - 1] print ("Sum of Right Diagonal is:", right_diagonal) T = [[ 11, 12, 33, 24 ], [ 54, 69, 72, 84 ], [ 14, 22, 63, 34 ], [ 53, 64, 79, 83 ]] sumofrightdiagonal (T, 4)
Output
Sum of Right Diagonal: 171
in conclusion
In this article, we briefly discussed two simple ways to calculate the right diagonal sum of a matrix using Python programs. The first method uses two loops to accomplish the task we provided, while the second method provides a more efficient way to accomplish the same task, but with a shorter path.
The above is the detailed content of Python program to calculate the sum of the right diagonal elements of a matrix. For more information, please follow other related articles on the PHP Chinese website!

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft