Objective

Create a function to transpose a given square matrix.

Difficulty

Easy

Description

In the world of matrices, a particularly useful operation is that of transposition.

Matrix transposition, as the name suggests, is to exchange the positions of elements such that the ::i^{th}:: row of the original matrix becomes the ::i^{th}:: column of the transposed matrix 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.

Note that the function MUST mutate the original array.

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

<?php

/* Functions defined here... */

$a = [ [1, 4], [10, 2] ];
print_matrix(transpose($a));
echo "\n";
print_matrix($a);
1 10 4 2 1 10 4 2