Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

การบวกเมทริกซ์ (Matrix Addition) ใน C#

การบวกเมทริกซ์ (Matrix Addition) ใน C#

การบวกเมทริกซ์ต้องมีขนาดเท่ากัน (จำนวนแถวและคอลัมน์เท่ากัน) บวกโดยนำองค์ประกอบตำแหน่งเดียวกันมาบวกกัน: C[i,j] = A[i,j] + B[i,j]

ตัวอย่างโค้ดครบถ้วน

using System;

class Demo
{
    static void Main()
    {
        Console.Write("ป้อนจำนวนแถว: ");
        int m = int.Parse(Console.ReadLine());
        Console.Write("ป้อนจำนวนคอลัมน์: ");
        int n = int.Parse(Console.ReadLine());

        int[,] mat1 = new int[m, n];
        int[,] mat2 = new int[m, n];
        int[,] result = new int[m, n];

        // รับเมทริกซ์ 1
        Console.WriteLine("ป้อนค่า Matrix 1:");
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
            {
                Console.Write($"mat1[{i},{j}] = ");
                mat1[i, j] = int.Parse(Console.ReadLine());
            }

        // รับเมทริกซ์ 2
        Console.WriteLine("ป้อนค่า Matrix 2:");
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
            {
                Console.Write($"mat2[{i},{j}] = ");
                mat2[i, j] = int.Parse(Console.ReadLine());
            }

        // บวกเมทริกซ์
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                result[i, j] = mat1[i, j] + mat2[i, j];

        // แสดงผล
        PrintMatrix("Matrix 1", mat1, m, n);
        PrintMatrix("Matrix 2", mat2, m, n);
        PrintMatrix("Matrix Addition Result", result, m, n);
    }

    static void PrintMatrix(string name, int[,] mat, int rows, int cols)
    {
        Console.WriteLine($"\n{name}:");
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
                Console.Write($"{mat[i, j],4}");
            Console.WriteLine();
        }
    }
}

ตัวอย่างการทำงาน

ป้อนจำนวนแถว: 2
ป้อนจำนวนคอลัมน์: 3
ป้อนค่า Matrix 1:
mat1[0,0] = 1  mat1[0,1] = 2  mat1[0,2] = 3
mat1[1,0] = 4  mat1[1,1] = 5  mat1[1,2] = 6

ป้อนค่า Matrix 2:
mat2[0,0] = 7  mat2[0,1] = 8  mat2[0,2] = 9
mat2[1,0] = 10 mat2[1,1] = 11 mat2[1,2] = 12

Matrix 1:
   1   2   3
   4   5   6

Matrix 2:
   7   8   9
  10  11  12

Matrix Addition Result:
   8  10  12
  14  16  18

กฎการบวกเมทริกซ์

  • เมทริกซ์ทั้งสองต้องมีขนาดเท่ากัน (m × n เท่ากัน)
  • บวกองค์ประกอบตำแหน่งเดียวกัน: C[i,j] = A[i,j] + B[i,j]
  • มีสมบัติการสลับที่ (Commutative): A + B = B + A
  • มีสมบัติการรวมกลุ่ม (Associative): (A + B) + C = A + (B + C)

ขยายความสามารถ: เมธอดย่อย


static int[,] AddMatrices(int[,] a, int[,] b)
{
    int rows = a.GetLength(0);
    int cols = a.GetLength(1);
    
    if (rows != b.GetLength(0) || cols != b.GetLength(1))
        throw new ArgumentException("Matrix dimensions must match");
    
    int[,] result = new int[rows, cols];
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            result[i, j] = a[i, j] + b[i, j];
    
    return result;
}

การดำเนินการเมทริกซ์อื่นๆ

  • ลบ: result[i,j] = mat1[i,j] - mat2[i,j]
  • คูณสเกลาร์: result[i,j] = mat1[i,j] * scalar
  • คูณเมทริกซ์: ต้องมี cols ของ A = rows ของ B, ใช้ลูป 3 ชั้น
  • ทรานสโพส (Transpose): สลับแถว-คอลัมน์ result[j,i] = mat[i,j]