diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 0861687..04bef9d 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -63,14 +63,18 @@ MatrixType getMatrixAt(const Matrix matrix, // Zwei Matrizen addieren Matrix add(const Matrix matrix1, const Matrix matrix2) { + // Dimensionen und Buffer überprüfen if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols || + matrix1.rows == 0 || + matrix1.cols == 0 || matrix1.buffer == NULL || matrix2.buffer == NULL) { return createMatrix(0, 0); } + // Ergebnismatrix erstellen Matrix result = createMatrix(matrix1.rows, matrix1.cols); if (result.buffer == NULL) @@ -78,21 +82,18 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) return result; } - for (unsigned int i = 0; i < matrix1.rows; i++) - { - for (unsigned int j = 0; j < matrix1.cols; j++) - { - MatrixType value = - getMatrixAt(matrix1, i, j) + - getMatrixAt(matrix2, i, j); + // Matrizen elementweise addieren + unsigned int size = matrix1.rows * matrix1.cols; - setMatrixAt(value, result, i, j); - } + for (unsigned int i = 0; i < size; i++) + { + result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i]; } return result; } + // Zwei Matrizen multiplizieren Matrix multiply(const Matrix matrix1, const Matrix matrix2) { @@ -123,6 +124,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) } setMatrixAt(sum, result, i, j); + } }