有没有办法创建 std::vector来自 arma::mat 矩阵而不创建矩阵的副本?

发布于 2025-01-18 18:16:51 字数 905 浏览 4 评论 0原文

我是c ++的新手。对于统计方法,我计算大型矩阵,例如 a 和b。它们是nx n,因此对于大型样本量n,它们变得非常大。如果它们是doublen = 70k,我认为它可能是按30GB的顺序? 由于所需的矩阵数量可能会有所不同,因此我实现了算法以使用矩阵向量并在其上迭代某些操作。例如,

arma::mat A;
arma::mat B;
std::vector<arma::mat> matrices;
matrices = {A, B};

有没有一种方法可以在不复制矩阵的情况下创建此std :: vector

我试图通过这样做来检查内存是否相同:

logger->info("Memory address for A: {}.", (void *)&A);
logger->info("Memory address for matrices.at(0): {}.", (void *)&matrices.at(0));

它显示了不同的地址,因此我认为它正在创建副本,但我不确定。

我尝试使用

std::vector<arma::mat> matrices;
matrices.push_back(A);

内存地址仍然不同。 由于矩阵是空的,因此使用

std::vector<arma::mat> matrices;
matrices.push_back(std::move(A));

算法不再起作用。

I am new to C++. For a statistical method, I compute large matrices, e.g. A and B . They are n x n so for large sample sizes n, they become very large. If they are double and n = 70k , I think it might be on the order of 30GB?
Because the number of matrices needed can vary, I implemented the algorithm to use a vector of matrices and iterate over it for some operations. E.g.

arma::mat A;
arma::mat B;
std::vector<arma::mat> matrices;
matrices = {A, B};

Is there a way to create this std::vector without copying the matrices?

I tried to check whether the memory is the same by doing this:

logger->info("Memory address for A: {}.", (void *)&A);
logger->info("Memory address for matrices.at(0): {}.", (void *)&matrices.at(0));

And it showed different addresses so I assume it is creating a copy but I am not sure.

I tried to use

std::vector<arma::mat> matrices;
matrices.push_back(A);

The memory addresses differed still. With

std::vector<arma::mat> matrices;
matrices.push_back(std::move(A));

the algorithm no longer worked because the matrices were empty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她比我温柔 2025-01-25 18:16:51

您需要逆转逻辑:让std :: vector分配内存并创建矩阵。然后,您直接与向量中的元素合作。例如:

std::vector<arma::mat> matrices;
matrices.resize(2);
arma::mat & A = matrices[0];
arma::mat & B = matrices[1];
// Initializing the values of A and B, and do stuff with them.

注意:重要的是要注意ab的引用,如果以后将更多元素添加到向量,即如果向量增长,则将变得无效。 。但是,由于这些矩阵太大,因此无论如何,您都希望不惜一切代价避免这种情况。

如果容器需要增长,您可能需要看一下EG std :: List

You need to reverse the logic: Let the std::vector allocate the memory and create the matrices. Then you work directly with the elements in the vector. For example:

std::vector<arma::mat> matrices;
matrices.resize(2);
arma::mat & A = matrices[0];
arma::mat & B = matrices[1];
// Initializing the values of A and B, and do stuff with them.

Note: It is important to note here that the references to A and B will become invalid if you add more elements afterwards to the vector, i.e. if the vector grows. But since these matrices are so large, you want to avoid this anyway at all costs.

If the container needs to grow, you might want to take a look at e.g. std::list.

止于盛夏 2025-01-25 18:16:51

首先,提前申请内存。
二、使用高级构造函数:
mat(ptr_aux_mem, n_rows, n_cols, copy_aux_mem = false, strict = false)

First, apply for memory in advance.
Second, use advanced constructors:
mat(ptr_aux_mem, n_rows, n_cols, copy_aux_mem = false, strict = false)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文