簡體   English   中英

(Rcpp,犰狳)將arma :: vec轉換為arma :: mat

[英](Rcpp, armadillo) convert arma::vec to arma::mat

我有一個矩陣X,由arma::vectorise函數向量化。 在對轉換后的向量x進行一些計算之后,我想將其重塑為arma::mat 我嘗試在Armadillo中使用.reshape函數,但它給了我這個錯誤。

RCPP代碼

// [[Rcpp::export]]
arma::mat vec2mat(arma::vec x, int nrow, int ncol){
  return x.reshape(nrow, ncol);
}

錯誤信息

no viable conversion from returned value of type 'void' to function return type 'arma::mat' (aka 'Mat<doubld>')

有人會幫助我找到解決這個問題的好方法嗎? 我不確定在這種情況下應該為函數返回類型使用哪種類型。 如果您知道將向量轉換為矩陣的另一種方法,那也很好:)

提前致謝!

您忽略/忽略了Armadillo文檔中的詳細信息: reshape()已存在的矩陣的成員函數,而您嘗試通過賦值來強制使用它。 而且編譯器不會告訴您mas 因此,請聽編譯器。

工作代碼

#include <RcppArmadillo.h>

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

// [[Rcpp::export]]
arma::mat vec2mat(arma::vec x, int nrow, int ncol) {
  arma::mat y(x);
  y.reshape(nrow, ncol);
  return y;
}

演示版

> Rcpp::sourceCpp("56606499/answer.cpp")  ## filename I used
> vec2mat(sqrt(1:10), 2, 5)
         [,1]     [,2]     [,3]     [,4]     [,5]
[1,] 1.000000 1.732051 2.236068 2.645751 3.000000
[2,] 1.414214 2.000000 2.449490 2.828427 3.162278
> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM