generated from freudenreichan/info2Praktikum-NeuronalesNetz
Matrixpart
This commit is contained in:
1 parent
9f3fc14182
commit
5d5a51b805
3 files changed
+69
-19
No files matched your search
+60
-16
@@ -2,34 +2,78 @@
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
// TODO Matrix-Funktionen implementieren (done)
|
||||
//Matrix Zeilen und Spaltenanzahl festlegen
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
|
||||
Matrix matrix;
|
||||
matrix.rows = rows;
|
||||
matrix.cols = cols;
|
||||
clearMatrix(&matrix);
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
//Matrix Speicher leeren
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
for (int i=0; i < (*matrix).rows; i++)
|
||||
{
|
||||
for (int j = 0; j < (*matrix).cols; j++)
|
||||
{
|
||||
(*matrix).array[j][i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Matrix Werte setzen
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if (rowIdx < 28 && colIdx < 28)
|
||||
{
|
||||
matrix.array[rowIdx][colIdx] = value;
|
||||
}
|
||||
}
|
||||
|
||||
//Matrix Werte auslesen
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if (rowIdx < 28 && colIdx < 28)
|
||||
{
|
||||
return matrix.array[rowIdx][colIdx];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
//Matrizen addieren
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
if ((matrix1.rows != matrix2.rows) || (matrix1.cols != matrix2.cols)) //Überprüfung der Addierbarkeit zweier Matrizen
|
||||
{
|
||||
return result;
|
||||
}
|
||||
for (int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for (int j = 0; j < matrix1.cols; j++)
|
||||
{
|
||||
result.array[i][j] = matrix1.array[i][j] + matrix2.array[i][j];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
//Matrizen multiplizieren
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
||||
if (matrix1.cols != matrix2.rows) //Überprüfung der Multiplizierbarkeit zweier Matrizen
|
||||
{
|
||||
return result;
|
||||
}
|
||||
for (int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for (int j = 0; j < matrix2.cols; j++)
|
||||
{
|
||||
for (int k = 0; k < matrix1.cols; k++)
|
||||
{
|
||||
result.array[i][j] += (matrix1.array[i][k] * matrix2.array[k][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user