Transpose Matrix

This algorithm finds the transpose of a given matrix. The transpose of a given matrix is formed by interchanging the rows and columns of a matrix.



									class Matrix
{
	public $_Matrix;
	public $Rows;
	public $Columns;
}

function TransposeMatrix($matrix)
{
	$transpose = array();

	for ($i = 0; $i < $matrix->Rows; $i++)
		for ($j = 0; $j < $matrix->Columns; $j++)
			$transpose[$j][$i] = $matrix->_Matrix[$i][$j];

	$retVal = new Matrix();
	$retVal->_Matrix = $transpose;
	$retVal->Rows = $matrix->Columns;
	$retVal->Columns = $matrix->Rows;
	return $retVal;
}
								


Example

									$m1 = array();
$m1[0][0] = 1;
$m1[0][1] = 2;
$m1[0][2] = 3;
$m1[1][0] = 4;
$m1[1][1] = 5;
$m1[1][2] = 6;

$matrix = new Matrix();
$matrix->_Matrix = $m1;
$matrix->Rows = 2;
$matrix->Columns = 3;

$transpose = TransposeMatrix($matrix);
								


Output

									[0][0] = 1
[0][1] = 4
[1][0] = 2
[1][1] = 5
[2][0] = 3
[2][1] = 6