Kotlin Program to Find Transpose of a Matrix

Finding the transpose of a matrix involves swapping rows with columns in a two-dimensional array. This article describes three different Kotlin Program to Find Transpose of a Matrix providing real examples with varying solutions and outputs.

1. Using Nested Loops

1.1 Example 1: Finding Transpose of a 3×3 Matrix

Kotlin
fun main() {
    val matrix = arrayOf(
        intArrayOf(1, 2, 3),
        intArrayOf(4, 5, 6),
        intArrayOf(7, 8, 9)
    )
    
    val transpose = Array(matrix[0].size) { IntArray(matrix.size) }
    
    for (i in matrix.indices) {
        for (j in matrix[i].indices) {
            transpose[j][i] = matrix[i][j]
        }
    }
    
    println("Original Matrix:")
    printMatrix(matrix)
    
    println("\nTranspose Matrix:")
    printMatrix(transpose)
}

fun printMatrix(matrix: Array<IntArray>) {
    for (row in matrix) {
        println(row.joinToString())
    }
}

Output:

Kotlin
Original Matrix:
1, 2, 3
4, 5, 6
7, 8, 9

Transpose Matrix:
1, 4, 7
2, 5, 8
3, 6, 9

2. Using transpose Function

2.1 Example 2: Finding Transpose of a 2×4 Matrix

Kotlin
fun main() {
    val matrix = arrayOf(
        intArrayOf(1, 2, 3, 4),
        intArrayOf(5, 6, 7, 8)
    )
    
    val transpose = matrix.transpose()
    
    println("Original Matrix:")
    printMatrix(matrix)
    
    println("\nTranspose Matrix:")
    printMatrix(transpose)
}

fun printMatrix(matrix: Array<IntArray>) {
    for (row in matrix) {
        println(row.joinToString())
    }
}

fun Array<IntArray>.transpose(): Array<IntArray> {
    val rows = this.size
    val cols = this[0].size
    
    val transposedMatrix = Array(cols) { IntArray(rows) }
    
    for (i in 0 until rows) {
        for (j in 0 until cols) {
            transposedMatrix[j][i] = this[i][j]
        }
    }
    
    return transposedMatrix
}

Output:

Kotlin
Original Matrix:
1, 2, 3, 4
5, 6, 7, 8

Transpose Matrix:
1, 5
2, 6
3, 7
4, 8

3. Using Extension Function

3.1 Example 3: Finding Transpose of a 4×2 Matrix

Kotlin
fun main() {
    val matrix = arrayOf(
        intArrayOf(1, 2),
        intArrayOf(3, 4),
        intArrayOf(5, 6),
        intArrayOf(7, 8)
    )
    
    val transpose = matrix.transpose()
    
    println("Original Matrix:")
    printMatrix(matrix)
    
    println("\nTranspose Matrix:")
    printMatrix(transpose)
}

fun printMatrix(matrix: Array<IntArray>) {
    for (row in matrix) {
        println(row.joinToString())
    }
}

fun Array<IntArray>.transpose(): Array<IntArray> {
    val rows = this.size
    val cols = this[0].size
    
    val transposedMatrix = Array(cols) { IntArray(rows) }
    
    for (i in 0 until rows) {
        for (j in 0 until cols) {
            transposedMatrix[j][i] = this[i][j]
        }
    }
    
    return transposedMatrix
}

Output:

Kotlin
Original Matrix:
1, 2
3, 4
5, 6
7, 8

Transpose Matrix:
1, 3, 5, 7
2, 4, 6, 8

Explanation and Summary

  • Example 1: Uses nested loops to find the transpose of a matrix.
  • Example 2: Defines a transpose extension function to transpose a matrix efficiently.
  • Example 3: Demonstrates how extension functions can be used to handle different matrix sizes while finding the transpose.

These examples showcase various approaches to finding the transpose of a matrix in Kotlin, highlighting the flexibility and readability offered by Kotlin’s language features. Whether using loops for explicit manipulation or extension functions for concise transformations, Kotlin provides efficient solutions for matrix operations.