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.



									public struct Matrix
{
	public int[,] _Matrix;
	public int Rows;
	public int Columns;

	public Matrix(int[,] matrix, int rows, int columns)
	{
		this._Matrix = matrix;
		this.Rows = rows;
		this.Columns = columns;
	}
}

public static Matrix TransposeMatrix(Matrix matrix)
{
	int[,] transpose = new int[matrix.Columns, matrix.Rows];

	for (int i = 0; i < matrix.Rows; ++i)
		for (int j = 0; j < matrix.Columns; ++j)
			transpose[j, i] = matrix._Matrix[i, j];

	return new Matrix(transpose, matrix.Columns, matrix.Rows);
}
								


Example

									int[,] m1 = new int[2, 3];
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 matrix = new Matrix(m1, 2, 3);
Matrix transpose = TransposeMatrix(matrix);
								


Output

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