Rcpp:将矩阵转换为向量

标签 r rcpp

通常我想将一个二维矩阵从 Rcpp 转换为 R 中的向量,使用“as(m)”应该非常简单,但是,我仍然从 R 中得到一个矩阵,我想知道为什么?我应该在 Rcpp 中手动删除 attr 吗?

#include <Rcpp.h>
#include <string>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector matrix2vector(NumericMatrix m, const bool byrow=false){
  NumericVector x;
  if(byrow){
    Rcout<< "warning: default by column\n";
    m = transpose(m);
    x = as<NumericVector>(m);
  }else{
    x = as<NumericVector>(m);
  }
  return(x);
}

/*** R
m=matrix(1:15,5,3)
matrix2vector(m,byrow=T)
*/

最佳答案

我不确定我是否理解问题或尝试的答案。您仍在此处返回矩阵。并且 Rcpp 对 reshape 和更高级的矩阵操作的支持有限。

代码的略微简化版本:

代码
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector matrix2vector(NumericMatrix m, const bool byrow=false){
  if (byrow){
    Rcout << "warning: default by column\n";
    m = transpose(m);
  }
  return NumericVector(m);
}

/*** R
m <- matrix(1:15,5,3)
print(matrix2vector(m, byrow = TRUE))
print(matrix2vector(m, byrow = FALSE))
*/
输出
R> Rcpp::sourceCpp("~/git/stackoverflow/61036707/question.cpp")

R> m <- matrix(1:15,5,3)

R> print(matrix2vector(m, byrow = TRUE))
warning: default by column
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    6    7    8    9   10
[3,]   11   12   13   14   15

R> print(matrix2vector(m, byrow = FALSE))
     [,1] [,2] [,3]
[1,]    1    6   11
[2,]    2    7   12
[3,]    3    8   13
[4,]    4    9   14
[5,]    5   10   15
R> 

实际上,您在这里执行的只是转置。

我会尝试 RcppArmadillo,它确实具有明确的 rowveccolvec 类型,后者被别名为 vec。我将不得不检查它在将“按列行”元素 reshape 为按列向量的行方面的建议。

编辑:确实(通常)在 RcppArmadillo 中更容易。

代码
#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
arma::mat matrix2vector(arma::mat m, const bool byrow=false){
  if (byrow) {
    return m.as_row();
  } else {
    return m.as_col();
  }
}

/*** R
m <- matrix(1:15,5,3)
print(matrix2vector(m, byrow = TRUE))
print(matrix2vector(m, byrow = FALSE))
*/
输出
R> Rcpp::sourceCpp("~/git/stackoverflow/61036707/answer.cpp")

R> m <- matrix(1:15,5,3)

R> print(matrix2vector(m, byrow = TRUE))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,]    1    6   11    2    7   12    3    8   13     4     9    14     5    10    15

R> print(matrix2vector(m, byrow = FALSE))
      [,1]
 [1,]    1
 [2,]    2
 [3,]    3
 [4,]    4
 [5,]    5
 [6,]    6
 [7,]    7
 [8,]    8
 [9,]    9
[10,]   10
[11,]   11
[12,]   12
[13,]   13
[14,]   14
[15,]   15
R> 

请注意,我使用 arma::mat 作为返回类型,以免与 colvecrowvec 冲突。您可以选择其中一个,但之后您将返回答案。

编辑 2: 根据下面的评论:如果您真的只想 reshape 您已有的内容,则不需要 Rcpp。只需核对 dim 属性即可。在 R(和 C++,如果需要)级别工作:

R> m
     [,1] [,2] [,3]
[1,]    1    6   11
[2,]    2    7   12
[3,]    3    8   13
[4,]    4    9   14
[5,]    5   10   15
R> dim(m)
[1] 5 3
R> dim(m) <- NULL
R> m
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
R> 

关于Rcpp:将矩阵转换为向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61036707/

相关文章:

r - 非线性混合模型: what am I doing wrong?

r - 相当于 Rcpp 中的 'which' 函数

c++ - 尝试使用外部库时 undefined reference

从 R 调用 C 函数

r - 如何保存在 Shiny 的应用程序中制作的绘图

r - 在 R 中将多元 XTS 转换为 TS

r - 在 R 中使用 Plotly 进行子图

r - 更新到 macOS Catalina 后无法使用 C++ 代码编译 R 包

r - 当因子水平只有一个水平时,将 predict() 与 RcppArmadillo/RcppEigen 结合使用

c++ - 重复 Rcpp NumericVector