Trusted answers to developer questions

Educative Answers Team

A $N$x$N$ matrix is a **Toeplitz matrix** if each descending diagonal, from left to right, is constant. This means that all the elements of the diagonal are the same.

A matrix (`Mat`

) is a Toeplitz matrix if Mat_{[i][j]} is the same as Mat_{[i+1][j+1]}, Mat_{[i+2][j+2]}, and so on.

For each element $[i][j]$ in our matrix, check the value of its immediate diagonal located at $[i+1][j+1]$. If we find an element whose value differs from that of its immediate diagonal, then the matrix is not a Toeplitz and we return `false`

.

A Toeplitz matrix

#include <iostream> using namespace std; // Matrix dimensions #define M 4 #define N 4 // Function to determine if given matrix is Toeplitz matrix // or not bool checkToepliz(int matrix[M][N]) { for (int i = 0; i < M - 1; i++) for (int j = 0; j < N - 1; j++) // return false if any diagonal elements have different values if (matrix[i][j] != matrix[i + 1][j + 1]) return false; return true; } int main() { int matrix[M][N] = { { 3, 7, 0, 9 }, { 5, 3, 7, 0 }, { 6, 5, 3, 7 }, { 4, 6, 5, 3 } }; if (checkToepliz(matrix)) cout << "Is a Toepliz matrix."; else cout << "Not a Toepliz matrix."; return 0; }

RELATED TAGS

toeplitz

matrix

c++

python

Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring

Related Courses