Python program to display upper triangular matrix
A matrix is a two-dimensional array consisting of many numbers arranged in rows and columns. A square matrix (one whose rows and columns have the same number of elements) has two diagonals. One is the main diagonal - located from the upper left corner to the lower right corner of the square. The second is the auxiliary diagonal - located from the upper right corner to the lower left corner.
For a square matrix, if all elements below the main diagonal are zero, it is called upper triangular matrix.
[1, 3, 4] [0, 5, 6] [0, 0, 3]
If the given matrix is not a square matrix, the matrix cannot be converted to an upper triangular matrix.
Input and output scenarios
Suppose we have a square matrix. The output matrix will be an upper triangular matrix.
Input matrix: [1, 3, 5, 7] [9, 2, 4, 2] [6, 3, 1, 4] [5, 8, 7, 6] Upper triangular matrix: [1, 3, 5, 7] [0, 2, 4, 2] [0, 0, 1, 4] [0, 0, 0, 6]
Let us see the following example to display an upper triangular matrix. We will use python list of lists to create the matrix.
Example
In this example, we will display the upper triangular matrix by replacing the lower triangular elements (4, 8, 1) with zeros.
arr = [[1, 2, 3], [4, 5, 6], [1, 8, 5]] print("The original matrix: ") for row in arr: print(row) print() print("The upper triangular matrix: ") if(len(arr) != len(arr[0])): print("Matrix should be a square matrix"); else: for i in range(3): for j in range(3): if(i<=j): print(arr[i][j],end=" ") else: print(0,end=" ") print()
Output
The original matrix: [1, 2, 3] [4, 5, 6] [1, 8, 5] The upper triangular matrix: 1 2 3 0 5 6 0 0 5
Example
In this example we only show the upper triangular matrix. Instead of converting the lower try element to zero.
arr = [[1, 2, 3], [4, 5, 6], [1, 8, 5]] print("The original matrix: ") for row in arr: print(row) print() print("The upper triangular matrix: ") for i in range(3): for j in range(3): if(i > j): print(end=" ") else: print(arr[i][j],end=" ") print(" ")
Output
The original matrix: [1, 2, 3] [4, 5, 6] [1, 8, 5] The upper triangular matrix: 1 2 3 5 6 5
Example
In this example, we will update the original matrix by converting the lower triangle elements to zero, and then we will display the upper triangle matrix.
arr = [[1, 2, 3], [4, 5, 6], [1, 8, 5]] print("The original matrix: ") for row in arr: print(row) print() print("The upper triangular matrix: ") for i in range(3): for j in range(3): if(i > j): arr[i][j] = 0 print(arr[i][j],end=" ") else: print(arr[i][j],end=" ") print(" ")
Output
The original matrix: [1, 2, 3] [4, 5, 6] [1, 8, 5] The upper triangular matrix: 1 2 3 0 5 6 0 0 5
The above is the detailed content of Python program to display upper triangular matrix. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Using Notepad++ to run a Python program requires the following steps: 1. Install the Python plug-in; 2. Create a Python file; 3. Set the run options; 4. Run the program.

PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

PyCharm is a powerful Python integrated development environment that provides a wealth of functions and tools to help developers improve efficiency. Among them, PyInstaller is a commonly used tool that can package Python code into an executable file (EXE format) to facilitate running on machines without a Python environment. In this article, we will introduce how to use PyInstaller in PyCharm to package Python code into EXE format, and provide specific

Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

What happens when the desktop layout is locked? When using the computer, sometimes we may encounter the situation where the desktop layout is locked. This problem means that we cannot freely adjust the position of desktop icons or change the desktop background. So, what exactly is going on when it says that the desktop layout is locked? 1. Understand the desktop layout and locking functions. First, we need to understand the two concepts of desktop layout and desktop locking. Desktop layout refers to the arrangement of various elements on the desktop, including shortcuts, folders, widgets, etc. we can be free

Llama3 is here! Just now, Meta’s official website was updated and the official announced Llama 38 billion and 70 billion parameter versions. And it is an open source SOTA after its launch: Meta official data shows that the Llama38B and 70B versions surpass all opponents in their respective parameter scales. The 8B model outperforms Gemma7B and Mistral7BInstruct on many benchmarks such as MMLU, GPQA, and HumanEval. The 70B model has surpassed the popular closed-source fried chicken Claude3Sonnet, and has gone back and forth with Google's GeminiPro1.5. As soon as the Huggingface link came out, the open source community became excited again. The sharp-eyed blind students also discovered immediately

The Python program development process includes the following steps: Requirements analysis: clarify business needs and project goals. Design: Determine architecture and data structures, draw flowcharts or use design patterns. Writing code: Program in Python, following coding conventions and documentation comments. Testing: Writing unit and integration tests, conducting manual testing. Review and Refactor: Review code to find flaws and improve readability. Deploy: Deploy the code to the target environment. Maintenance: Fix bugs, improve functionality, and monitor updates.

Flask installation and configuration tutorial: A tool to easily build Python Web applications, specific code examples are required. Introduction: With the increasing popularity of Python, Web development has become one of the necessary skills for Python programmers. To carry out web development in Python, we need to choose a suitable web framework. Among the many Python Web frameworks, Flask is a simple, easy-to-use and flexible framework that is favored by developers. This article will introduce the installation of Flask framework,
