c++ - Armadillo C++ : Passing a submatrix into a function

标签 c++ armadillo

我正在使用 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/

相关文章:

c++ - 构造函数内部的 Printf 未显示在控制台中

openmp - 用于矩阵向量乘积的 Rcpp Parallel 或 openmp

matrix - 如何将立方体 reshape 为矩阵 Armadillo

c++ - 使用编译器安装库

c++ - 将 Armadillo 添加到 Qt 项目

c++ - ArrayFire:具有从主函数调用的 OpenCL 内核的函数

c++ - Qt creator - 在新窗口中显示的 .ui 表单

c++ - 将字符串转换为字节并发送()它

c++ - 子列表 C++ 的返回迭代器

c++ - 从单个数组的不同段初始化 Eigen::Vector