To avoid or reduce copying, you can access the memory used by Armadillo matrices through a function . memptr () . For instance:
mat X(5,6); double* mem = X.memptr();
Be careful when using the above, as you are not allowed to free memory on your own (Armadillo will still manage the memory).
Alternatively, you can directly build the Armadillo matrix from existing memory. For instance:
double* data = new double[4*5]; // ... fill data ... mat X(data, 4, 5, false); // 'false' indicates that no copying is to be done; see docs
In this case, you will be responsible for manually managing the memory.
Also keep in mind that Armadillo stores and accesses matrices in the column ordinal , i.e. column 0 is stored first, then column 1, column 2, etc. This is the same as used by MATLAB, LAPACK and BLAS.
mtall source share