Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

Wednesday, 20 December 2017

Matrix Construction in C++ using Armadillo

Matrix Construction in C++ using Armadillo

As a follow-through of vector, we go on matrix construction. Starting from the definition, matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. The individual items in an matrix often denoted by or so-called elements, where is row index and is column index.

Because of there is no container for matrix in C/C++ standard library, let’s make our way construction matrix. In C we could use pointer of array or pointer of pointer as follows :

int m = 4, n = 3, i;

// Using pointer of array
int *B[m];
for (i = 0; i < m; i++) {
    A[i] = (int *) calloc(n, sizeof(int));
}

// Using pointer of pointer
int **B = (int **) calloc(m, sizeof(int *));
for (i = 0; i < m; i++) {
    B[i] = (int *) calloc(n, sizeof(int)); 
}