A x 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 in our matrix, check the value of its immediate diagonal located at . 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
.
#include <iostream>using namespace std;// Matrix dimensions#define M 4#define N 4// Function to determine if given matrix is Toeplitz matrix// or notbool 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 valuesif (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.";elsecout << "Not a Toepliz matrix.";return 0;}
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved.