Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数组矩阵的结果存储到较小的数组c中#_C#_Arrays_Class_Math_Matrix - Fatal编程技术网

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

C# 如何将数组矩阵的结果存储到较小的数组c中#,c#,arrays,class,math,matrix,C#,Arrays,Class,Math,Matrix,我需要在矩阵中一个值的周围四个邻域中添加一个值,该值可以是p1(回报1)或p2(回报2),然后将其打印到一个新的数组矩阵中。如果它是1,则需要将p1添加到它的邻居;如果它是0,则将p2添加到它的邻居。我曾尝试使用嵌套的for循环来实现这种方法,但for循环中的“if”语句给了我错误,我不确定下一步该怎么做 class MainClass { static void Main(string[] args) { int m, n, i, j, p1, p2;

我需要在矩阵中一个值的周围四个邻域中添加一个值,该值可以是p1(回报1)或p2(回报2),然后将其打印到一个新的数组矩阵中。如果它是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();
        }
class类main类
{
静态void Main(字符串[]参数)
{
int m,n,i,j,p1,p2;
//矩阵的行和列+
m=3;
n=3;
//支付矩阵
p1=10;//欺骗支付矩阵
p2=5;//合作报酬矩阵
int[,]arr=新的int[3,3];
控制台。写入(“输入1表示合作”);
Console.Write(“输入0意味着作弊”);
Console.Write(“输入矩阵元素:”);
对于(i=0;i

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

如果我理解正确,您需要先制作一份数组副本。因为否则您将从当前迭代的数组位置(I+1)读取例如“1”。这可能不是您想要的。 然后,您只需在for循环中设置所需的值。您需要进行一些绑定检查,因为只有当i>0时,才能访问例如arrNew[i-1]

这会给你一些类似的东西:

        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 1 0
1 1 1
0 1 0

啊,是的,我现在明白了。克隆工作是怎么做的?如果我要做PayOfMatx= ARR.C克隆,它应该是这样的。而不是使用克隆函数,你可以迭代你的数组并手动将这些值复制到具有相同维度的新数组。如果它对你有帮助,请考虑接受答案。谢谢。为了寻求帮助,我现在正在努力打印支付矩阵中的值?我是否只需要一个控制台。外部for循环中的writeLine?整理了我之前的问题。我想最后问一下,是否有人知道如何将新矩阵中的值随机替换到原始数组中1。你应该迭代数组并打印每个值值(write/writeline)2.签出Random.Next(n)()这将为您提供一个介于0和n之间的伪随机数。此数字可用于在数组arr[rand.Next(3),rand.Next(3)]中生成地址。类似于此。
0 1 0
1 1 1
0 1 0