Common Matrix Operations
Teil des Abschnitts Logik & Ablauf der C#-Journey von Coddy — Lektion 6 von 66.
Matrizen werden häufig in der Mathematik und Informatik verwendet. Lassen Sie uns einige gängige Operationen auf zweidimensionalen Arrays erkunden.
Zwei Matrizen addieren:
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;
}Eine Matrix transponieren (Zeilen und Spalten austauschen):
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;
}Berechnen Sie die Summe jeder Zeile:
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;
}Zwei Matrizen multiplizieren:
Bei der Matrizenmultiplikation wird jedes Element
result[i][j] als das Skalarprodukt der Zeile i aus der ersten Matrix und der Spalte j aus der zweiten Matrix berechnet — das heißt, die Summe von matrix1[i][k] * matrix2[k][j] für jedes k. Die erste Matrix muss genauso viele Spalten haben wie die zweite Matrix Zeilen.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;
}Aufgabe
SchwerErstellen Sie eine Methode namens multiplyMatrices, die:
- Zwei Matrizen (2D-gezackte Arrays) als Parameter entgegennimmt: matrix1 und matrix2
- Sie gemäß den Regeln der Matrizenmultiplikation multipliziert
- Die resultierende Matrix zurückgibt
Für eine gültige Matrizenmultiplikation:
- Die Anzahl der Spalten in matrix1 muss der Anzahl der Zeilen in matrix2 entsprechen
- Das Ergebnis hat die Dimensionen: [matrix1.rows × matrix2.columns]
Wie die Matrizenmultiplikation funktioniert:
Jedes Element an der Position [i][j] im Ergebnis wird berechnet, indem die Zeile i aus matrix1 und die Spalte j aus matrix2 genommen werden, ihre entsprechenden Elemente miteinander multipliziert und alle diese Produkte summiert werden:result[i][j] = matrix1[i][0] * matrix2[0][j] + matrix1[i][1] * matrix2[1][j] + ...
Mit anderen Worten: result[i][j] = sum of (matrix1[i][k] * matrix2[k][j]) für jedes k.
Zum Beispiel, wenn matrix1 ist:
[1, 2]
[3, 4]Und matrix2 ist:
[5, 6]
[7, 8]Dann result[0][0] = 1*5 + 2*7 = 19, result[0][1] = 1*6 + 2*8 = 22 usw. Das Ergebnis sollte sein:
[19, 22]
[43, 50]Wenn die Matrizen nicht multipliziert werden können, geben Sie null zurück.
Spickzettel
Häufige Matrixoperationen mit 2D gezackten Arrays:
Zwei Matrizen addieren:
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;
}Eine Matrix transponieren (Zeilen und Spalten vertauschen):
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;
}Die Summe jeder Zeile berechnen:
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;
}Zwei Matrizen multiplizieren:
Jedes Element result[i][j] ist die Summe der Produkte der Zeile i aus der ersten Matrix und der Spalte j aus der zweiten Matrix:
result[i][j] += matrix1[i][k] * matrix2[k][j] für jedes k.
Die erste Matrix muss genauso viele Spalten haben wie die zweite Matrix Zeilen hat.
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;
}Probier es selbst
public class MultiplyMatrices
{
// Implementieren Sie die MultiplyMatrices-Methode
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2)
{
// Schreiben Sie Ihren Code hier
}
}Diese Lektion enthält ein kurzes Quiz. Starte die Lektion, um es zu beantworten und deinen Fortschritt zu speichern.
Alle Lektionen in Logik & Ablauf
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety