我正在使用 C++ 和 Armadillo 库。我有一个如下形式的函数
arma::mat foo(arma::mat my_matrix)
我想将 my_matrix
的子矩阵传递给 foo
。
子矩阵可以这样提取:
my_matrix.col(0)
但是每当我尝试这样做时:
foo(my_matrix.col(0))
我收到编译错误。 如何在不这样做的情况下修复它:
mat submatrix = my_matrix.col(0)
foo(submatrix)
(确实有效,但涉及一行额外的代码)。
最佳答案
将输入类型更改为 const 引用 (const arma::mat&
),这样编译器可以在这种情况下自动生成临时矩阵。
示例:
using namespace arma;
mat foo(const mat& my_matrix)
{
mat y = my_matrix * 2.0;
return y;
}
void bar()
{
mat x(10,10, fill::randu);
mat y = foo( x.col(0) );
y.print("y:");
}
关于c++ - Armadillo C++ : Passing a submatrix into a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27786970/