Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Matrix-transposition/Kotlin/matrix-transposition.kotlin
Normal file
24
Task/Matrix-transposition/Kotlin/matrix-transposition.kotlin
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// version 1.1.3
|
||||
|
||||
typealias Vector = DoubleArray
|
||||
typealias Matrix = Array<Vector>
|
||||
|
||||
fun Matrix.transpose(): Matrix {
|
||||
val rows = this.size
|
||||
val cols = this[0].size
|
||||
val trans = Matrix(cols) { Vector(rows) }
|
||||
for (i in 0 until cols) {
|
||||
for (j in 0 until rows) trans[i][j] = this[j][i]
|
||||
}
|
||||
return trans
|
||||
}
|
||||
|
||||
// Alternate version
|
||||
typealias Matrix<T> = List<List<T>>
|
||||
fun <T> Matrix<T>.transpose(): Matrix<T> {
|
||||
return (0 until this[0].size).map { x ->
|
||||
(this.indices).map { y ->
|
||||
this[y][x]
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue