Transpose Matrix
This algorithm finds the transpose of a given matrix. The transpose of a given matrix is formed by interchanging the rows and columns of a matrix.
Public Structure Matrix
Public _Matrix As Integer(,)
Public Rows As Integer
Public Columns As Integer
Public Sub New(matrix As Integer(,), rows As Integer, columns As Integer)
Me._Matrix = matrix
Me.Rows = rows
Me.Columns = columns
End Sub
End Structure
Public Shared Function TransposeMatrix(matrix As Matrix) As Matrix
Dim transpose As Integer(,) = New Integer(matrix.Columns - 1, matrix.Rows - 1) {}
For i As Integer = 0 To matrix.Rows - 1
For j As Integer = 0 To matrix.Columns - 1
transpose(j, i) = matrix._Matrix(i, j)
Next
Next
Return New Matrix(transpose, matrix.Columns, matrix.Rows)
End Function
Example
Dim m1 As Integer(,) = New Integer(1, 2) {}
m1(0, 0) = 1
m1(0, 1) = 2
m1(0, 2) = 3
m1(1, 0) = 4
m1(1, 1) = 5
m1(1, 2) = 6
Dim matrix As New Matrix(m1, 2, 3)
Dim transpose As Matrix = TransposeMatrix(matrix)
Output
(0, 0) = 1
(0, 1) = 4
(1, 0) = 2
(1, 1) = 5
(2, 0) = 3
(2, 1) = 6