Menu
Coddy logo textTech

Common Matrix Operations

Coddy C# 여정의 논리 및 흐름 섹션에 포함된 레슨 — 66개 중 6번째.

행렬은 수학과 컴퓨터 과학에서 흔히 사용됩니다. 2차원 배열에 대한 일반적인 연산 몇 가지를 살펴보겠습니다.

두 행렬을 더하기:

int[][] AddMatrices(int[][] a, int[][] b)
{
    int rows = a.Length;
    int[][] result = new int[rows][];
    
    for (int i = 0; i < rows; i++)
    {
        result[i] = new int[a[i].Length];
        for (int j = 0; j < a[i].Length; j++)
        {
            result[i][j] = a[i][j] + b[i][j];
        }
    }
    return result;
}

행렬 전치 (행과 열을 교환):

int[][] Transpose(int[][] matrix)
{
    int rows = matrix.Length;
    int cols = matrix[0].Length;
    
    int[][] result = new int[cols][];
    for (int i = 0; i < cols; i++)
    {
        result[i] = new int[rows];
        for (int j = 0; j < rows; j++)
        {
            result[i][j] = matrix[j][i];
        }
    }
    return result;
}

각 행의 합을 계산하세요:

int[] RowSums(int[][] matrix)
{
    int rows = matrix.Length;
    int[] sums = new int[rows];
    
    for (int i = 0; i < rows; i++)
    {
        int sum = 0;
        for (int j = 0; j < matrix[i].Length; j++)
        {
            sum += matrix[i][j];
        }
        sums[i] = sum;
    }
    return sums;
}

두 행렬을 곱하기:


행렬 곱셈에서, 각 요소 result[i][j]는 첫 번째 행렬의 행 i와 두 번째 행렬의 열 j내적으로 계산됩니다 — 즉, 모든 k에 대해 matrix1[i][k] * matrix2[k][j]의 합입니다. 첫 번째 행렬은 두 번째 행렬이 가진 만큼 을 가져야 합니다.

int[][] MultiplyMatrices(int[][] a, int[][] b)
{
    int rows = a.Length;
    int cols = b[0].Length;
    int inner = b.Length;
    
    int[][] result = new int[rows][];
    for (int i = 0; i < rows; i++)
    {
        result[i] = new int[cols];
        for (int j = 0; j < cols; j++)
        {
            int sum = 0;
            for (int k = 0; k < inner; k++)
            {
                sum += a[i][k] * b[k][j];
            }
            result[i][j] = sum;
        }
    }
    return result;
}
challenge icon

챌린지

어려움

multiplyMatrices라는 메서드를 생성하세요. 이 메서드는:

  1. 두 개의 행렬(2D jagged 배열)을 매개변수로 받습니다: matrix1 및 matrix2
  2. 행렬 곱셈 규칙에 따라 곱합니다
  3. 결과 행렬을 반환합니다

행렬 곱셈이 유효하려면:

  • matrix1의 열 수가 matrix2의 행 수와 같아야 합니다
  • 결과는 [matrix1.rows × matrix2.columns] 차원을 가집니다

행렬 곱셈 작동 방식:
결과에서 [i][j] 위치의 각 요소는 matrix1의 행 i와 matrix2의 열 j를 가져와 해당 요소들을 곱하고 모든 곱셈 결과를 합산하여 계산됩니다:

result[i][j] = matrix1[i][0] * matrix2[0][j] + matrix1[i][1] * matrix2[1][j] + ...

즉: 각 k에 대해 result[i][j] = sum of (matrix1[i][k] * matrix2[k][j]).

예를 들어, matrix1이 다음과 같다면:

[1, 2]
[3, 4]

matrix2가 다음과 같다면:

[5, 6]
[7, 8]

그러면 result[0][0] = 1*5 + 2*7 = 19, result[0][1] = 1*6 + 2*8 = 22 등이 됩니다. 결과는 다음과 같아야 합니다:

[19, 22]
[43, 50]

행렬을 곱할 수 없는 경우 null을 반환하세요.

치트 시트

2차원 가변 배열을 사용한 일반적인 행렬 연산:

두 행렬을 더하기:

int[][] AddMatrices(int[][] a, int[][] b)
{
    int rows = a.Length;
    int[][] result = new int[rows][];
    
    for (int i = 0; i < rows; i++)
    {
        result[i] = new int[a[i].Length];
        for (int j = 0; j < a[i].Length; j++)
        {
            result[i][j] = a[i][j] + b[i][j];
        }
    }
    return result;
}

행렬 전치 (행과 열 교환):

int[][] Transpose(int[][] matrix)
{
    int rows = matrix.Length;
    int cols = matrix[0].Length;
    
    int[][] result = new int[cols][];
    for (int i = 0; i < cols; i++)
    {
        result[i] = new int[rows];
        for (int j = 0; j < rows; j++)
        {
            result[i][j] = matrix[j][i];
        }
    }
    return result;
}

각 행의 합 계산:

int[] RowSums(int[][] matrix)
{
    int rows = matrix.Length;
    int[] sums = new int[rows];
    
    for (int i = 0; i < rows; i++)
    {
        int sum = 0;
        for (int j = 0; j < matrix[i].Length; j++)
        {
            sum += matrix[i][j];
        }
        sums[i] = sum;
    }
    return sums;
}

두 행렬 곱하기:
각 요소 result[i][j]는 첫 번째 행렬의 행 i와 두 번째 행렬의 열 j의 곱의 합입니다:
result[i][j] += matrix1[i][k] * matrix2[k][j]k에 대해.
첫 번째 행렬의 열 수가 두 번째 행렬의 행 수와 같아야 합니다.

int[][] MultiplyMatrices(int[][] a, int[][] b)
{
    int rows = a.Length;
    int cols = b[0].Length;
    int inner = b.Length;
    
    int[][] result = new int[rows][];
    for (int i = 0; i < rows; i++)
    {
        result[i] = new int[cols];
        for (int j = 0; j < cols; j++)
        {
            for (int k = 0; k < inner; k++)
            {
                result[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    return result;
}

직접 해보기

public class MultiplyMatrices
{
    // MultiplyMatrices 메서드를 구현하세요
    public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2)
    {
        // 여기에 코드를 작성하세요
        
    }
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

논리 및 흐름의 모든 레슨