高效copying/casting大矩阵std::vector<std::vector<double>>和犰狳arma::mat

Efficient copying/casting of large matrix std::vector<std::vector<double>> and armadillo arma::mat

有没有高效的复制机制

            std::vector<std::vector<double>> std_mat 
            arma::mat arma_mat

其中 arma::mat arma_mat 指犰狳 matrix/math 库。

http://arma.sourceforge.net/

我的项目依赖于两个单独的 matrix/data 采集库,其中矩阵分别如上定义。但是,在处理管道的某个阶段,我需要将一个复制到另一个以避免破坏遗留代码。我想知道是否存在从一个到另一个的某种转换运算符(因此我们不必复制),或者如果没有,一种有效的复制机制(类似于 vector.emplace_back 概念)。现在我正在使用双 for 循环,但我相信它会更有效率。

您可以通过稍微改变您使用 vectors/memory 的方式来避免复制。

如果你看 documentation

mat(ptr_aux_mem, n_rows, n_cols, copy_aux_mem = true, strict = false)

Create a matrix using data from writable auxiliary (external) memory, where ptr_aux_mem is a pointer to the memory. By default the matrix allocates its own memory and copies data from the auxiliary memory (for safety). However, if copy_aux_mem is set to false, the matrix will instead directly use the auxiliary memory (ie. no copying); this is faster, but can be dangerous unless you know what you are doing!

如果您知道矩阵的大小是固定的,则可以通过提供一块内存来构建 mat。请注意文档中说:

Elements are stored with column-major ordering (ie. column by column)