Skip to content
Snippets Groups Projects
Commit 51ab7843 authored by dario.genga's avatar dario.genga
Browse files

Add matrix transpose

parent 5201518a
No related branches found
No related tags found
No related merge requests found
......@@ -11,24 +11,33 @@
#include "matrix.h"
int main() {
matrix mat, cloned;
matrix mat, cloned, transposed;
int32_t m = 3;
int32_t n = 4;
int32_t s = m * n;
int32_t val = 0;
int32_t data[12] = { 2, 1, -1, -2, 3, 1, 1, 3, 1, 4, -1, -1 };
// Init
matrix_init(&mat, m, n, val);
printf("Empty matrix %dx%d\n", m, n);
matrix_print(mat);
// Init from array
matrix_init_from_array(&mat, m, n, data, s);
printf("Matrix %dx%d from array\n", m, n);
matrix_print(mat);
// Clone
matrix_clone(&cloned, mat);
printf("Cloned matrix :\n");
matrix_print(cloned);
// Transpose
matrix_transpose(&transposed, mat);
printf("Transposed matrix :\n");
matrix_print(transposed);
// Free the memory
matrix_destroy(&mat);
matrix_destroy(&cloned);
matrix_destroy(&transposed);
return EXIT_SUCCESS;
}
......@@ -106,3 +106,19 @@ error_code matrix_destroy(matrix *mat) {
return ok;
}
error_code matrix_transpose(matrix *transposed, const matrix mat) {
error_code code = matrix_alloc(transposed, mat.n, mat.m);
if (code == err) {
return err;
}
for (int i = 0; i < mat.m; i++) {
for (int k = 0; k < mat.n; k++) {
transposed->data[k][i] = mat.data[i][k];
}
}
return ok;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment