Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to check if a matrix is Toeplitz or not in TypeScript

1 Answer

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

function isToeplitz(matrix: number[][]) {
    const rows: number = matrix.length;
    const cols: number = matrix[0].length;
    
    for (let i: number = 1; i < rows; i++) {
        for (let j: number = 1; j < cols; j++) {
            console.log(i + " " + j + " - " + matrix[i][j] + " : " + matrix[i - 1][j - 1]);
            if (matrix[i][j] != matrix[i - 1][j - 1]) {
                return false;
            }
        }
        console.log("-----------");
    }
    
    return true;
}
        
const matrix: number[][] = [
            [2, 7, 9, 8], 
            [4, 2, 7, 9], 
            [3, 4, 2, 7], 
            [0, 3, 4, 2], 
            [6, 0, 3, 4]];
    
    if (isToeplitz(matrix)) {
        console.log("Matrix is a Toeplitz");
    }
    else {
        console.log("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

Related questions

1 answer 72 views
1 answer 81 views
1 answer 97 views
1 answer 77 views
1 answer 88 views
1 answer 75 views
1 answer 88 views
...