Objective

Transpose a given square matrix.

Difficulty

Easy

Description

In the world of matrices, yet another useful operation is that of transposition.

Let's see what exactly is it...

Matrix transposition, as the name suggests, is to exchange the positions of elements such that the ::i^{th}:: row becomes the ::i^{th}:: column and the ::j^{th}:: column becomes the ::j^{th}:: row.

The new matrix obtained after transposing a given matrix ::\text{A}:: is called the transpose of ::\text{A}::, and is denoted as ::\text{A}^{\text{t}}::.

For instance, if the matrix ::\text{A}:: is as follows:

::\begin{bmatrix} 1 & 4 \\ 10 & 2 \end{bmatrix}::

then its tranpose ::\text{A}^{\text{t}}:: would be the following:

::\begin{bmatrix} 1 & 10 \\ 4 & 2 \end{bmatrix}::

Similarly, if the matrix ::\text{A}:: is as follows:

::\begin{bmatrix} 3 & 5 & 6 \\ 10 & 4 & 2 \\ 1 & 7 & 8 \end{bmatrix}::

then its tranpose ::\text{A}^{\text{t}}:: would be the following:

::\begin{bmatrix} 3 & 10 & 1 \\ 5 & 4 & 7 \\ 6 & 2 & 8 \end{bmatrix}::

In this exercise, you have to create a function called transpose() that tranposes a given square matrix.

The matrix is provided as an argument to the function which then returns back its tranpose:

function transpose(matrix) {
   // your code
}

Note that the function must mutate the original array.

Shown below is how the function transpose() should behave in a couple of cases:

transpose([[1, 4], [10, 2]])
[[1, 10], [4, 2]]
transpose([[3, 5, 6], [10, 4, 2], [1, 7, 8]])
[[3, 10, 1], [5, 4, 7], [6, 2, 8]]
transpose([[2]])
[[2]]