How to check if a matrix is Toeplitz or not in VB.NET

1 Answer

0 votes
Imports System

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

Public Class Program
    Public Shared Function isToeplitz(ByVal matrix As Integer()()) As Boolean
        Dim rows As Integer = matrix.Length
        Dim cols As Integer = matrix(0).Length

        For i As Integer = 1 To rows - 1
            For j As Integer = 1 To cols - 1
                Console.WriteLine(i & " " & j & " - " & matrix(i)(j) & " : " & matrix(i - 1)(j - 1))

                If matrix(i)(j) <> matrix(i - 1)(j - 1) Then
                    Return False
                End If
            Next

            Console.WriteLine("-----------")
        Next

        Return True
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim matrix As Integer()() = New Integer()() {
			New Integer() {2, 7, 9, 8}, 
			New Integer() {4, 2, 7, 9}, 
			New Integer() {3, 4, 2, 7}, 
			New Integer() {0, 3, 4, 2}, 
			New Integer() {6, 0, 3, 4}}

        If isToeplitz(matrix) Then
            Console.Write("Matrix is a Toeplitz")
        Else
            Console.Write("Matrix is not a Toeplitz")
        End If
    End Sub
End Class






' 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 101 views
1 answer 112 views
1 answer 126 views
1 answer 101 views
1 answer 127 views
1 answer 103 views
1 answer 134 views
...