Golang Program To Find The Transpose Of A Matrix


In this article, we will write a go language program to find the transpose of a matrix. A matrix is a collection of numbers arranged in rows and columns, a two-dimensional array. Transpose of a matrix is defined as a matrix obtained by interchanging the rows and columns of that matrix.

Method 1: Find the Transpose of a Matrix using For Loop

In this example, we will write a go language program to find the transpose of the matrix using for loops in the main function.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Call the main() function.

Step 3 − Initialize a 2-d array called matrixA and matrixB and store elements in it.

Step 4 − Print the matrices on the screen.

Step 5 − To find the transpose of these matrices iterate over the matrix using for loop and make a new matrix from the old one by changing each row of the matrix to the column.

Step 6 − Once we have iterated over the matrix. Print the new matrix thus formed on the screen.

Step 7 − Repeat the process the for the second matrix and print the transpose on the screen using fmt.Println() function.

Example

Golang program to find the transpose of a matrix using for loop

package main
import (
   "fmt"
)
func main() {
   var i, j int
   var transposeMat [3][3]int
   matrixA := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   matrixB := [3][3]int{
      {10, 11, 12},
      {14, 15, 16},
      {18, 19, 10},
   }
   fmt.Println("The first matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(matrixA[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         transposeMat[j][i] = matrixA[i][j]
      }
   }
   fmt.Println("The transpose of matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(transposeMat[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   fmt.Println("The second matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(matrixB[i][j], "\t")
      }
      fmt.Println()
   }
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         transposeMat[j][i] = matrixB[i][j]
      }
   }
   fmt.Println()
   fmt.Println("The transpose of matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(transposeMat[i][j], "\t")
      }
      fmt.Println()
   }
}

Output

The first matrix is:
0  1  2
4  5  6
8 9  10
The transpose of matrix is:
0  4  8
1  5  9
2  6 10
The second matrix is:
10  11  12
14  15  16
18  19  10
The transpose of matrix is:
10  14  18
11  15  19
12  16  10

Method 2: Find the Transpose of a Matrix using the Range Function

Let us now look at another method to find the transpose of a matrix in the go programming language. In this method, we will use a user-defined function to achieve the result. This function will accept the matrix as an argument and will return the result to the main program.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Create a function to find the transpose of the given matrix called transpose().

Step 3 − This function uses two for loops to iterate over the matrix. At every iteration of the matrix, we are updating the elements of the new matrix by interchanging the rows and columns of the matrix received.

Step 4 − Once all the iterations are completed, return the result.

Step 5 − Now, start the main() function. Initialize a matrix of integer type and store values to them. Further, print these matrices on the screen.

Step 6 − Call the transpose() function by passing the matrix as arguments to the function and storing the result

Step 7 − Print the final result obtained on the screen using fmt.Println() function.

Example

Golang program to find the transpose of a matrix using the range function.

package main
import (
   "fmt"
)

// creating a function to find transpose
func transpose(matrixA [3][3]int) [3][3]int {
   var transposeMat [3][3]int
   for i, rows := range matrixA {
      for j := range rows {
         transposeMat[j][i] = matrixA[i][j]
      }
   }
   return transposeMat
}
func main() {
   var i, j int
   var transposeMat [3][3]int
   matrixA := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   fmt.Println("The matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(matrixA[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()

   // calling the transpose function
   transposeMat = transpose(matrixA)
   fmt.Println("The transpose of above matrix is:")
   for i, rows := range matrixA {
      for j := range rows {
         fmt.Print(transposeMat[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
}

Output

The matrix is:
0  1  2
4  5  6
8  9 10

The transpose of above matrix is:
0  4  8
1  5  9
2  6 10

Conclusion

We have successfully compiled and executed a golang program to find the transpose of a matrix along with examples. We are using two examples in this. In the first example, we have used for loops in the main() function while in the second one we are passing the matrix in a user-defined function to achieve its transpose.

Updated on: 06-Jan-2023

481 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements