4

我有以下数组:

int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } };
int[,] myArray2 = new int[2, 3] { { 6, 4, 3 }, { 8, 2, 8 } };

我想知道该怎么做是:

  1. 用 myArray1 和 myArray2 之和创建一个新数组
  2. 用 myArray1 和 myArray2 的减法创建一个新数组
  3. 使用 myArray1 和 myArray2 的乘法创建一个新数组

sum 的结果是:

int[,] myArray3 = new int[2, 3] { { 7, 6, 0 }, { -4, 4, 0 } };

减法的结果是:

int[,] myArray3 = new int[2, 3] { { 5, 2, 6 }, { 12, 8, 16 } };

乘法的结果将是:

int[,] myArray3 = new int[2, 3] { { 6, 8, 9 }, { 32, 12, 64 } };

这可以类似于使用 for 循环打印出数组吗?我尝试寻找示例,但没有找到可用于解决特定问题的示例。

4

5 回答 5

4
int[,] a3 = new int[2,3];

for(int i = 0; i < myArray1.GetLength(0); i++)
{
for(int j = 0; j < myArray1.GetLength(1); j++)
{
a3[i,j] = myArray1[i,j] + myArray2[i,j];
a3[i,j] = myArray1[i,j] - myArray2[i,j];
a3[i,j] = myArray1[i,j] * myArray2[i,j];
}
}

need to store a3 before doing a new calculation obviously

于 2012-12-18T08:17:05.760 回答
3

For Sum:

for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                myArray3[i, j] = myArray1[i, j] + myArray2[i, j];
            }                
        }

For Subtraction:

for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                myArray3[i, j] = myArray2[i, j] - myArray1[i, j];
            }                
        }

For Multiplication:

for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                myArray3[i, j] = A[i, j] * B[i, j];
            }
        }
于 2012-12-18T08:15:15.593 回答
1

是的,这就像用 for 循环打印出数组一样

c# 有 foreach 循环,这将更容易使用

注意:我知道这是为了家庭作业,所以我不会给出一个 100% 的结论性的全部答案。

 int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } };
 int[,] myArray2 = new int[2, 3] { { 6, 4, 3 }, { 8, 2, 8 } };
    
          foreach (int[] a1 in myArray1) 
          {
             foreach(int i in a1)
             {
                //operation here
                //you get the idea
             }        
          }
于 2012-12-18T07:41:30.017 回答
1

If you want to do array manipulation faster use the C# Parallel.For loop from System.Threading.Tasks:

For simple arithmetic parallelizing the outer loop is much faster than not on a modern PC processor. For more complex operations, or for small array sizes, the parallel version can be slower for various reasons.

Thus, use a stopwatch to time your matrix operations, and use the fastest solution. Parallelization makes doing array / image processing in C# much faster if implemented right.

Beware of overflowing your datatypes after arithmetic operations and also sharing variables between multiple threads (see System.Threading.Interlocked for help with that)...

Subtraction below. Similar for addition and multiplication:

Parallel.For(0, array.GetLength(1), y=>
{
    for (int x = 0; x < array.GetLength(0); x++)
        {
            difference[x,y] = minuend[x,y] - subtrahend[x,y];
        }
    }
});
于 2021-02-26T23:56:17.550 回答
0

If you want to use a for loop, you can iterate through the rows/columns of the multi-d array as follows:

for (int i = 0; i < myArray1.GetLength(0); i++)
{
    for (int j = 0; j < myArray1.GetLength(1); j++)
    {
        // Here, you can access the array data by index, using i and j. 
        // Ex, myArray1[i, j] will give you the value of 1 in the first iteration.
    }
}

Note: When you pass a value into the Array's GetLength method, it represents the dimension of the array. See http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx

于 2012-12-18T07:56:03.350 回答