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

Add sub matrix

parent 51ab7843
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "matrix.h" #include "matrix.h"
int main() { int main() {
matrix mat, cloned, transposed; matrix mat, cloned, transposed, sub;
int32_t m = 3; int32_t m = 3;
int32_t n = 4; int32_t n = 4;
int32_t s = m * n; int32_t s = m * n;
...@@ -33,10 +33,15 @@ int main() { ...@@ -33,10 +33,15 @@ int main() {
matrix_transpose(&transposed, mat); matrix_transpose(&transposed, mat);
printf("Transposed matrix :\n"); printf("Transposed matrix :\n");
matrix_print(transposed); matrix_print(transposed);
// Sub matrix
matrix_extract_submatrix(&sub, transposed, 1, 4, 1, 3);
printf("Sub matrix :\n");
matrix_print(sub);
// Free the memory // Free the memory
matrix_destroy(&mat); matrix_destroy(&mat);
matrix_destroy(&cloned); matrix_destroy(&cloned);
matrix_destroy(&transposed); matrix_destroy(&transposed);
matrix_destroy(&sub);
return EXIT_SUCCESS; return EXIT_SUCCESS;
......
...@@ -122,3 +122,21 @@ error_code matrix_transpose(matrix *transposed, const matrix mat) { ...@@ -122,3 +122,21 @@ error_code matrix_transpose(matrix *transposed, const matrix mat) {
return ok; return ok;
} }
error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int32_t m0, int32_t m1, int32_t n0, int32_t n1) {
if (m0 < 0 || m1 > mat.m || n0 < 0 || n1 > mat.n || m0 > m1 || n0 > n1) {
return err;
}
error_code code = matrix_alloc(sub, m1 - m0 , n1 - n0 );
if (code == err) {
return err;
}
for (int i = m0; i < m1; i++) {
for (int k = n0; k < n1; k++) {
sub->data[i - m0][k - n0] = 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