How to check if a matrix is Toeplitz or not in C++

2 Answers

0 votes
#include <iostream>

// A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements

#define COLS 4
 
bool isDiagonalSameValue(int matrix[][COLS], int i, int j, int rows, int cols) {
    int value = matrix[i][j];
    
    while (++i < rows && ++j < cols) {
        std::cout << i << " " << j <<  " - " << matrix[i][j] << "\n";
        if (matrix[i][j] != value) {
            return false;
        }
    }
    
    std::cout << "-------" << "\n";
    
    return true;
}
 
bool isToeplitz(int matrix[][COLS], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        if (!isDiagonalSameValue(matrix, 0, i, rows, cols)) {
            return false;
        }
    }
 
    for (int j = 1; j < cols; j++) {
        if (!isDiagonalSameValue(matrix, j, 0, rows, cols)) {
            return false;
        }
    }
 
    return true;
}
 
int main()
{
    int matrix[][COLS] = { 
            { 2, 7, 9, 8 },
            { 4, 2, 7, 9 },
            { 3, 4, 2, 7 },
            { 0, 3, 4, 2 },
            { 6, 0, 3, 4 }};
 
    int rows = (sizeof(matrix) / sizeof(matrix[0]));
    int cols = (sizeof(matrix) / sizeof(matrix[0][0])) / rows;
     
    if (isToeplitz(matrix, rows, cols)) {
        std::cout << "Matrix is a Toeplitz";
    }
    else {
        std::cout << "Matrix is not a Toeplitz";
    }
}
 
 
 
      
/*
run:
      
1 1 - 2
2 2 - 2
3 3 - 2
-------
1 2 - 7
2 3 - 7
-------
1 3 - 9
-------
-------
-------
2 1 - 4
3 2 - 4
4 3 - 4
-------
3 1 - 3
4 2 - 3
-------
4 1 - 0
-------
Matrix is a Toeplitz
     
*/

 



answered Jan 23, 2024 by avibootz
edited Jan 23, 2024 by avibootz
0 votes
#include <iostream>

// A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements

#define COLS 4
 
bool isToeplitz(int matrix[][COLS], int rows, int cols) {
    for (int i = 1; i < rows; i++) {
        for (int j = 1; j < cols; j++) {
            std::cout << i << " " << j <<  " - " << matrix[i][j] << " : " << matrix[i - 1][j - 1] << "\n";
            if (matrix[i][j] != matrix[i - 1][j - 1]) {
                return false;
            }
        }
        std::cout << "-----------" << "\n";
    }
    
    return true;
}
 
int main()
{
    int matrix[][COLS] = { 
            { 2, 7, 9, 8 },
            { 4, 2, 7, 9 },
            { 3, 4, 2, 7 },
            { 0, 3, 4, 2 },
            { 6, 0, 3, 4 }};
 
    int rows = (sizeof(matrix) / sizeof(matrix[0]));
    int cols = (sizeof(matrix) / sizeof(matrix[0][0])) / rows;
     
    if (isToeplitz(matrix, rows, cols)) {
        std::cout << "Matrix is a Toeplitz";
    }
    else {
        std::cout << "Matrix is not a Toeplitz";
    }
}
 
 
 
      
/*
run:
      
1 1 - 2 : 2
1 2 - 7 : 7
1 3 - 9 : 9
-----------
2 1 - 4 : 4
2 2 - 2 : 2
2 3 - 7 : 7
-----------
3 1 - 3 : 3
3 2 - 4 : 4
3 3 - 2 : 2
-----------
4 1 - 0 : 0
4 2 - 3 : 3
4 3 - 4 : 4
-----------
Matrix is a Toeplitz
     
*/

 



answered Jan 23, 2024 by avibootz
edited Jan 23, 2024 by avibootz

Related questions

2 answers 122 views
1 answer 100 views
1 answer 112 views
1 answer 126 views
1 answer 100 views
1 answer 126 views
1 answer 103 views
...