Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Armadillo: Obtain raw data from matrix/vector as array

Tags:

c++

armadillo

I am searching for something like this:

fmat A     = randu<fmat>(4,5);
float **a  = A.to_array();     // Return matrix as float**

Does anybody know how one could do this in Armadillo?

like image 355
Stefan Falk Avatar asked May 08 '26 00:05

Stefan Falk


1 Answers

There is no function to return an array of pointers. You can access the underlying buffer with the memptr() method:

float *a = A.memptr();

You can also get a pointer to any matrix column with the colptr() method. I'm not sure why you might need an array of pointers but you could build one like this (uncompiled and untested code):

std::vector<float *> av;
av.reserve(A.n_cols);
for (unsigned int i = 0; i < A.n_cols; ++i)
   av.push_back() = A.colptr(i);

float **a = &av[0]; // a remains valid while av is in scope

Note that Armadillo stores data in column-major order.

like image 184
rhashimoto Avatar answered May 10 '26 13:05

rhashimoto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!