使用RcppArmadillo submat()通过引用传递的更新Rcpp :: NumericMatrix [英] Update Rcpp::NumericMatrix passed by reference using RcppArmadillo submat()

查看:89
本文介绍了使用RcppArmadillo submat()通过引用传递的更新Rcpp :: NumericMatrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照此问题,我是试图了解如何有效地更新 Rccp :: NumericMatrix 数据类型的子集.

Following on this question, I am trying to understand how to efficiently update a subset of a Rccp::NumericMatrix data type.

我有以下情况:

  • Rcpp :: NumericMatrix m 5 x 5 ,需要更新几行和几列.
  • 它将通过引用传递给函数(返回类型 void ),该函数会将其转换为 arma :: mat ,并更新相应的 submat().
  • 目前我还不了解如何将在函数内部发生的更改"应用"到传递给函数的 m 矩阵.
  • li>
  • Rcpp::NumericMatrix m of 5 x 5 that needs few rows and columns updated.
  • It will be passed by reference to a function (void return type) that will convert it to an arma::mat, and update the respective submat().
  • At this point I don't understand how to "apply" the changes that occurred inside the function to the m matrix that was passed to the function.

代码如下:

#include <iostream>
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]


// [[Rcpp::export]]
void updateMatrix(const Rcpp::NumericMatrix &m)
{
    std::cout << m << std::endl;

    Rcpp::as<arma::mat>(m).submat(0, 0, 3, 3) = Rcpp::as<arma::mat>(m).submat(0, 0, 3, 3) + 1;

    std::cout << m << std::endl;
}

要从 R 运行它,我使用:

m = matrix(0, 5, 5)

updateMatrix(m)

结果是:

> updateMatrix(m)
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000

0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000

这是我第一次使用 Rcpp RcppArmadillo ,它们绝对很棒.感谢您在这种情况下的帮助.

It's the first time I'm using Rcpp and RcppArmadillo and they are absolutely amazing. I appreciate any help with this scenario.

推荐答案

updateMatrix 中作业的左侧创建一个临时对象,该临时对象在分配后将被丢弃.因此, m 完全不变.该代码无法按您预期的那样工作,因为这将意味着 m 的类型将发生变化.看下面:

The left side of your assignment in updateMatrix creates a temporary that is discarded after assignment. Therefore, m doesn't change at all. The code can't work as you expected as the that would mean the type of m would change. Look below:

#include <typeinfo>
#include <iostream>
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]


// [[Rcpp::export]]
void updateMatrix(const Rcpp::NumericMatrix &m)
{
  std::cout << m << std::endl;

  std::cout << typeid(m).name() << std::endl;

  arma::mat m2 = Rcpp::as<arma::mat>(m);

  std::cout << typeid(m2).name() << std::endl;

  m2.submat(0, 0, 3, 3) = Rcpp::as<arma::mat>(m).submat(0, 0, 3, 3) + 1;

  std::cout << m2 << std::endl;
}

运行此代码将给出:

> m = matrix(0, 5, 5)
> updateMatrix(m)
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000

N4Rcpp6MatrixILi14ENS_15PreserveStorageEEE
N4arma3MatIdEE
   1.0000   1.0000   1.0000   1.0000        0
   1.0000   1.0000   1.0000   1.0000        0
   1.0000   1.0000   1.0000   1.0000        0
   1.0000   1.0000   1.0000   1.0000        0
        0        0        0        0        0

这篇关于使用RcppArmadillo submat()通过引用传递的更新Rcpp :: NumericMatrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆