Rcpp:将矩阵转换为向量

Rcpp: transform matrix to vector

一般来说,我想将一个二维矩阵从 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)
*/

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

您的代码的稍微简化的版本:

代码
#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/Whosebug/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。我将不得不检查它在将 "rows by columns" 元素按列向量重塑为行方面的建议。

编辑:在 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/Whosebug/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 作为 return 类型,以免与 colvecrowvec 冲突。您可以选择其中一个,但您可能 不是 return 答案。

编辑 2: 根据下面的评论:如果您真的只是想重塑您已经拥有的内容,则不需要 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> 

感谢@user2957945 和@Dirk Eddelbuettel。

我想要的是:

#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);
  }
  NumericVector x(m);
  x.attr("dim") = R_NilValue;
  return(x);
}

/*** R
m=matrix(1:15,5,3)
matrix2vector(m,byrow=T)
# [1]  1  6 11  2  7 12  3  8 13  4  9 14  5 10 15
*/