forked from freudenreichan/info2Praktikum-NeuronalesNetz
Grundkonzept Matrix
This commit is contained in:
parent
96c6871ea3
commit
8368439db1
49
matrix.c
49
matrix.c
@ -6,30 +6,67 @@
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
|
||||
// Allocate memory for the matrix structure
|
||||
Matrix matrix;
|
||||
matrix.rows = rows;
|
||||
matrix.cols = cols;
|
||||
matrix.data = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
|
||||
if (matrix.data == NULL)
|
||||
{
|
||||
// Handle memory allocation failure
|
||||
matrix.rows = 0;
|
||||
matrix.cols = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
// Free the allocated memory for the matrix data
|
||||
if (matrix->data != NULL)
|
||||
{
|
||||
free(matrix->data);
|
||||
matrix->data = NULL;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
// Set the value at the specified row and column index
|
||||
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
||||
{
|
||||
matrix.data[rowIdx * matrix.cols + colIdx] = value;
|
||||
}
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
// Get the value at the specified row and column index
|
||||
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
||||
{
|
||||
return matrix.data[rowIdx * matrix.cols + colIdx];
|
||||
}
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
// Add two matrices and return the result
|
||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||
{
|
||||
// Handle dimension mismatch
|
||||
Matrix emptyMatrix = createMatrix(0, 0);
|
||||
return emptyMatrix;
|
||||
}
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
// Multiply two matrices and return the result
|
||||
if (matrix1.cols != matrix2.rows)
|
||||
{
|
||||
// Handle dimension mismatch
|
||||
Matrix emptyMatrix = createMatrix(0, 0);
|
||||
return emptyMatrix;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user