Armadillo: convert cube subview (tube) to vector

Viewed 2152

I get compilation error when I try to get a vector/double from armadillo cube (using tube or slice). How to get from cube to (row)vec? I can not find an elegant cast/transform armadillo function.

#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.830123 << 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?
}
1 Answers
Related