How to transpose matrix?

Reza Agustina picture Reza Agustina · Apr 7, 2015 · Viewed 15.4k times · Source

I have made 8x8 matrix using c#, and now I need to transpose the matrix. Can you help me to transpose the matrix? This is my matrix

public double[,] MatriksT(int blok)
{
    double[,] matrixT = new double[blok, blok];

    for (int j = 0; j < blok ; j++)
    {
        matrixT[0, j] = Math.Sqrt(1 / (double)blok);

    }

    for (int i = 1; i < blok; i++)
    {
        for (int j = 0; j < blok; j++)
        {
            matrixT[i, j] = Math.Sqrt(2 / (double)blok) * Math.Cos(((2 * j + 1) * i * Math.PI) / 2 * blok);
        }
    }

    return matrixT;

}

Answer

jan picture jan · Apr 7, 2015
public double[,] Transpose(double[,] matrix)
{
    int w = matrix.GetLength(0);
    int h = matrix.GetLength(1);

    double[,] result = new double[h, w];

    for (int i = 0; i < w; i++)
    {
        for (int j = 0; j < h; j++)
        {
            result[j, i] = matrix[i, j];
        }
    }

    return result;
}