Skip to content

Instantly share code, notes, and snippets.

@claytonjwong
Created April 8, 2022 19:19
Show Gist options
  • Save claytonjwong/ed5617c0fcc8ddc6636312b754432cb1 to your computer and use it in GitHub Desktop.
Save claytonjwong/ed5617c0fcc8ddc6636312b754432cb1 to your computer and use it in GitHub Desktop.
transposes a matrix, ie. transform rows into columns

Kotlin

var transpose = { A: Array<IntArray> -> A[0].mapIndexed{ j, _ -> A.mapIndexed{ i, _ -> A[i][j] }.toIntArray() }.toTypedArray() }

Javascript

let transpose = A => A[0].map((_, j) => A.map((_, i) => A[i][j]));

Python3

transpose = lambda A: [[A[i][j] for i in range(len(A))] for j in range(len(A[0]))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment