How to check if a given matrix is a Toeplitz matrix

A NNxNN 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.

Algorithm

For each element [i][j][i][j] in our matrix, check the value of its immediate diagonal located at [i+1][j+1][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.

5237852378521785
A Toeplitz matrix

Implementation

#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;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved