1
當我嘗試從犰狳立方體(使用管或切片)獲取矢量/雙精度時,出現編譯錯誤。如何從立方體到(行)vec?我找不到優雅的演員/變形犰狳功能。犰狳:將立方體子視圖(管)轉換爲矢量
#include <RcppArmadillo.h>
//#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
void testarma() {
arma::mat B;
B << 0.555950 << 0.274690 << 0.540605 << 0.798938 << arma::endr
<< 0.108929 << 0.83<< 0.891726 << 0.895283 << arma::endr
<< 0.948014 << 0.973234 << 0.216504 << 0.883152 << arma::endr
<< 0.023787 << 0.675382 << 0.231751 << 0.450332 << arma::endr;
B.print("B:");
// cubes ("3D matrices")
arma::cube Q(B.n_rows, B.n_cols, 4);
Q.slice(0) = B;
Q.slice(1) = 2.0 * B;
Q.slice(2) = 3.0 * B;
Q.slice(3) = 4.0 * B;
Q.print("Q:");
Rcpp::Rcout << "Q.tube(0, 1): " << std::endl << Q.tube(0, 1) << std::endl;
arma::rowvec whatIwant = arma::rowvec(4).fill(0.0);
for (int i = 0; i < 4; i++) {
// This gives an error. cannot convert 'arma::subview_col<double>' to 'double' in assignment
//whatIwant[i] = Q.slice(i).row(0).col(1);
Rcpp::Rcout << "At slice i=" << i << " value=" << Q.slice(i).row(0).col(1) << std::endl;
}
// But I don't want to write for loop. I am sure there exists
// an "armadillo" way (function) that does exactly this
// How to convert output of tube to rowvec?
}
請示例代碼可重複.... – coatless
@coatless:很想,但它必須數百行代碼。問題是如何獲得arma :: rowvec作爲i,j元素(在我的情況下(0,1))來自armadillo cube的所有片的每個矩陣。 – Samo
你可以在這種情況下創建一個玩具的例子嗎? – coatless