Matrix noch ohne broadcasting

This commit is contained in:
Max-R 2025-11-20 16:03:44 +01:00
parent f9c46a6784
commit 0886489d49

View File

@ -44,16 +44,38 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
// Ergebnismatrix
Matrix result;
const int cols1 = matrix1.cols;
const int rows1 = matrix1.rows;
const int cols2 = matrix2.cols;
const int rows2 = matrix2.rows;
const int rowsEqual = (matrix1.rows==matrix2.rows) ? 1: 0;
const int colsEqual = (matrix1.cols==matrix2.cols) ? 1: 0;
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassender
// Matrix
if (matrix1.rows != matrix2.rows) {
// check, which one is smaller
// realloc
if (rowsEqual == 1 && colsEqual == 1){
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
for (int i = 0; i< rows1; i++) {
for (int j= 0; j< cols1; j++){
int valueM1= getMatrixAt(matrix1, i, j);
int valueM2= getMatrixAt(matrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
return result;
}
else if (rowsEqual ==1 && colsEqual == 0){
}
else if (rowsEqual == 0 && colsEqual == 1){
}
else {
if (matrix1.cols != matrix2.cols) {
}
// Speicher reservieren