c# - 打印出矩阵及其转置,C#

标签 c# algorithm matrix transpose

我有一个创建转置 5x8 矩阵的程序。我创建了一个多维 5x8 数组,还创建了一个新数组来保存多维数组的转置。问题是,我首先想将原始矩阵写出到控制台,并且在同一行我希望写出转置。这是我的代码:

 class Program
{
    static void Main(string[] args)
    {


        int[,] matrix = 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 },

        };

        for (int j = 0; j < 8; j++)
        {
            for (int r = 0; r < 5; r++)
                Console.Write("{0} ", matrix[r, j]);

            Console.WriteLine();
        }

        int[,] newArray = new int[8, 5];
        for (int j = 0; j < 8; j++)
            for (int r = 0; r < 5; r++)
                newArray[j, r] = matrix[r, j];




        Console.ReadLine();

    }
}

我想要在控制台窗口中显示的是这样的: http://pbrd.co/19SXR0J

但我只能打印出转置矩阵。我该如何解决这个问题?

最佳答案

计算转置后,您可以同时打印两行。

 for (int j = 0; j < 8; j++)
    {
        //write a line from the first matrix
        for (int r = 0; r < 5; r++)
            Console.Write("{0} ", matrix[r, j]);

        //add some spaces for visual separation
        Console.Write("\t\t");

        //write a line from the transpose matrix
        for (int r = 0; r < 5; r++)
            Console.Write("{0} ", newArray[r, j]);

        Console.WriteLine();
    }

关于c# - 打印出矩阵及其转置,C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18431695/

相关文章:

c# - 格式化字符串但省略参数(如果它为空)

c# - 如何使用字典引用动态类型?

c# - 删除字符串中的特殊字符和无效字符

algorithm - 计算函数求和的高效算法

algorithm - 为什么考虑 NP 而不是 P?

python - 如何在 Python 上的 PCA 中创建相关矩阵?

c# - 使用 XPath 和 C# 移动多个 iFrame

c# - 顺时针排序点列表

c - 下三角矩阵在 0 时给出错误答案

python - 使用元组/列表作为数组索引 (Python)