如何将数组矩阵的结果存储到较小的数组c#

Posted

技术标签:

【中文标题】如何将数组矩阵的结果存储到较小的数组c#【英文标题】:How to store the results of an array matrix into a smaller array c# 【发布时间】:2020-06-05 02:16:42 【问题描述】:

我需要在矩阵中一个值的周围四个邻居中添加一个值,该值可以是 p1(支付一)或 p2(支付二),然后将打印到一个新的数组矩阵中。如果它是 1,那么 p1 将需要添加到它的邻居中,或者如果它是 0,那么 p2 将被添加到它的邻居中。我尝试使用嵌套的 for 循环执行此方法,但我的 for 循环中的“if”语句给了我错误,我不确定下一步该去哪里。

  class MainClass

    static void Main(string[] args)
    
        int m, n, i, j, p1, p2;

        // rows and columns of the matrix+
        m = 3;
        n = 3;

        //Payoff matrix
        p1 = 10; //cheat payoff matrix
        p2 = 5; //co-op payoff matrix




        int[,] arr = new int[3, 3];


        Console.Write("To enter 1 it means to co-operate" );
        Console.Write("To enter 0 it means to cheat");

        Console.Write("Enter elements of the Matrix: ");
        for (i = 0; i < m; i++)
        
            for (j = 0; j < n; j++)
            
                arr[i, j] = Convert.ToInt16(Console.ReadLine());

            
        






        Console.WriteLine("Printing Matrix: ");
        for (i = 0; i < m; i++)
        
            for (j = 0; j < n; j++)
            
                Console.Write(arr[i, j] + "\t");
            
            Console.WriteLine();
        




        // how to change the values of the matrix

        int[] payoffMatrix = new int[4];

        for (i = 0; i < m; i++)
        
            for (j = 0; j < n; j++)
            
                if(arr[i,j] == 1)
                
                    arr[i, j] = arr[i - 1, j] , arr[i + 1, j] , arr[i, j - 1] , arr[i, j + 1];
                

            
            Console.WriteLine();
        

相邻值的结果也需要打印到支付矩阵中

【问题讨论】:

【参考方案1】:

如果我理解正确,您需要先复制您的数组。因为否则你会阅读例如“1”来自当前迭代的数组位置 (i + 1)。这可能不是你想要的。 然后,您只需在 for 循环中设置所需的值。你需要一些边界检查,因为例如arrNew[i - 1] 只有在 i > 0

时才能访问

这会给你类似的东西:

        int[,] arrNew = arr.Clone() as int[,]; //creates a copy of arr

        for (i = 0; i < m; i++)
        
            for (j = 0; j < n; j++)
            
                if (arr[i, j] == 1)
                
                    if (i > 0) //bounds checking
                    
                        arrNew[i - 1, j] = 1;
                    

                    if (i < m - 1) //bounds checking
                    
                        arrNew[i + 1, j] = 1;
                    

                    if (j > 0) //bounds checking
                    
                        arrNew[i, j - 1] = 1;
                    

                    if (j < n - 1) //bounds checking
                    
                        arrNew[i, j + 1] = 1;
                    

                

            
        

对于矩阵:

0 0 0
0 1 0
0 0 0

结果是

0 1 0
1 1 1
0 1 0

【讨论】:

啊,是的,我现在明白了。 .clone 是如何工作的?如果我要做 payoffMatrix = arr.clone ?会有用吗 应该的。您可以迭代数组并将值手动复制到具有相同维度的新数组中,而不是使用克隆函数。如果对您有帮助,请考虑接受答案。谢谢。 将接受作为答案,感谢您的帮助,我现在正在努力打印收益矩阵中的值?我只需要在外部 for 循环中有一个 console.writeLine 吗? 对我之前的问题进行了排序。最后我想问一下,有人知道如何将新矩阵中的值随机替换到原始数组中 1.您应该遍历您的数组并打印每个值(写入/写入行) 2. 检查 Random.Next(n) (docs.microsoft.com/en-us/dotnet/api/…) 这将为您提供一个介于 0 和 n 之间的伪随机数。此数字可用于在数组 arr[rand.next(3), rand.next(3)] 中生成地址。类似的东西。

以上是关于如何将数组矩阵的结果存储到较小的数组c#的主要内容,如果未能解决你的问题,请参考以下文章

数据结构-特殊矩阵的压缩存储

C# 将一个数组分成多个较小的数组(随机)

在 C# 中用较小的数组复制/填充大数组的最佳方法是啥?

c#保存二进制编码,是存储空间最小的方法?

如何将大数组拆分为较小的数组?

绘制到较小的图像时,drawInRect 会失去分辨率吗?