如何转置矩阵?

时间:2015-04-07 04:16:39

标签: c# matrix transpose

我使用c#制作了8x8矩阵,现在我需要转置矩阵。你能帮我转置矩阵吗? 这是我的矩阵

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;

}

3 个答案:

答案 0 :(得分:6)

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;
}

答案 1 :(得分:1)

public class Matrix<T>
{
    public static T[,] TransposeMatrix(T[,] matrix)
    {
        var rows    = matrix.GetLength(0);
        var columns = matrix.GetLength(1);

        var result = new T[columns, rows];

        for (var c = 0; c < columns; c++)
        {
            for (var r = 0; r < rows; r++)
            {
                result[c, r] = matrix[r, c];
            }
        }

        return result;
    }
}

这就是如何称呼它:

int[,] matris = new int[5, 8]
        {
            {1  , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
            {9  , 10, 11, 12, 13, 14, 15, 16},
            {17 , 18, 19, 20, 21, 22, 23, 24},
            {25 , 26, 27, 28, 29, 30, 31, 32},
            {33 , 34, 35, 36, 37, 38, 39, 40},

        };
var tMatrix = Matrix<int>.TransposeMatrix(matris);

答案 2 :(得分:0)

Transpose of Matrix in c#。您可以将它们保存在另一个矩阵中,而不是打印结果。 :)

相关问题