
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check whether given matrix is Toeplitz Matrix or not in Python
Suppose we have a matrix M, we have to check whether it is a Toeplitz matrix or not. As we know a matrix is said to be Toeplitz when every diagonal descending from left to right has the same value.
So, if the input is like
7 | 2 | 6 |
3 | 7 | 2 |
5 | 3 | 7 |
then the output will be True.
To solve this, we will follow these steps −
- for each row i except the last one, do
- for each column except the last one, do
- if matrix[i, j] is not same as matrix[i+1, j+1], then
- return False
- if matrix[i, j] is not same as matrix[i+1, j+1], then
- for each column except the last one, do
- return True
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, matrix): for i in range(len(matrix)-1): for j in range(len(matrix[0])-1): if matrix[i][j]!=matrix[i+1][j+1]: return False return True ob = Solution() matrix = [ [7, 2, 6], [3, 7, 2], [5, 3, 7]] print(ob.solve(matrix))
Input
[[7, 2, 6], [3, 7, 2], [5, 3, 7]]
Output
True
- Related Articles
- How to check whether the given matrix is a Toeplitz matrix using C#?
- Find if given matrix is Toeplitz or not in C++
- C Program To Check whether Matrix is Skew Symmetric or not?
- Program to check if a matrix is Binary matrix or not in C++
- How to check in R whether a matrix element is present in another matrix or not?
- C++ code to check given matrix is good or not
- Check given matrix is magic square or not in C++
- Program to check whether given graph is bipartite or not in Python
- C Program to check if matrix is singular or not
- Check if a given matrix is sparse or not in C++
- Check if a given matrix is Hankel or not in C++
- Python program to check whether a given string is Heterogram or not
- Check if a Matrix is Identity Matrix or not in Java?
- Check if a Matrix is Markov Matrix or not in Java
- Program to check whether given number is Narcissistic number or not in Python

Advertisements