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,950 questions

51,892 answers

573 users

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

2 Answers

0 votes
using System;

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

public class Program
{
	private static bool isDiagonalSameValue(int[][] matrix, int i, int j) {
		int rows = matrix.Length;
		int cols = matrix[0].Length;

		int value = matrix[i][j];

		while (++i < rows && ++j < cols) {
			Console.WriteLine(i + " " + j + " - " + matrix[i][j]);
			if (matrix[i][j] != value) {
				return false;
			}
		}

		Console.WriteLine("-------");

		return true;
	}

	private static bool isToeplitz(int[][] matrix) {
		int rows = matrix.Length;
		int cols = matrix[0].Length;

		for (int i = 0; i < rows - 1; i++) {
			if (!isDiagonalSameValue(matrix, 0, i)) {
				return false;
			}
		}

		for (int j = 1; j < cols; j++) {
			if (!isDiagonalSameValue(matrix, j, 0)) {
				return false;
			}
		}

		return true;
	}
	public static void Main(string[] args)
	{
		int[][] matrix = new int[][]
		{
			new int[] {2, 7, 9, 8},
			new int[] {4, 2, 7, 9},
			new int[] {3, 4, 2, 7},
			new int[] {0, 3, 4, 2},
			new int[] {6, 0, 3, 4}
		};

		if (isToeplitz(matrix)) {
			Console.Write("Matrix is a Toeplitz");
		}
		else {
			Console.Write("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
0 votes
using System;

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

public class Program
{
	private static bool isToeplitz(int[][] matrix) {
		int rows = matrix.Length;
		int cols = matrix[0].Length;

        for (int i = 1; i < rows; i++) {
            for (int j = 1; j < cols; j++) {
                Console.WriteLine(i + " " + j + " - " + matrix[i][j] + " : " + matrix[i - 1][j - 1]);
                if (matrix[i][j] != matrix[i - 1][j - 1]) {
                    return false;
                }
            }
            Console.WriteLine("-----------");
        }
        
        return true;
	}
	public static void Main(string[] args)
	{
		int[][] matrix = new int[][]
		{
			new int[] {2, 7, 9, 8},
			new int[] {4, 2, 7, 9},
			new int[] {3, 4, 2, 7},
			new int[] {0, 3, 4, 2},
			new int[] {6, 0, 3, 4}
		};

		if (isToeplitz(matrix)) {
			Console.Write("Matrix is a Toeplitz");
		}
		else {
			Console.Write("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 73 views
1 answer 81 views
1 answer 97 views
1 answer 77 views
1 answer 88 views
1 answer 75 views
1 answer 106 views
...