[Rcpp-devel] Variable for loop

Amina Shahzadi aminashahzadi at gmail.com
Mon Sep 26 23:59:09 CEST 2016


Hi Dear

I have a problem in using a variable for loop in using RcppArmadillo
library.
I have pasting here my code. It is executing but not giving the same
results as its R code version gives. The results produced by it are really
weird. I have checked it step by step. It is because of the for (int q=0;
q<i; q++). I request tp please help how to handle it in cpp.

The another question is I want to multiply the cube b(i, ,) by a scalar.
How can we consider the entire columns and slices of a cube for each of the
rows.  "b(span(i), span(), span())"  is not working for me.

Thank you

#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace RcppArmadillo;
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
arma::cube up(arma::mat a){
  int m = a.n_cols;
  int n = a.n_rows;
  int p = a.n_rows;
  arma::cube b(n, m, p);
  for(int i=0; i<n; i++){
      for(int j=0; j<m; j++){
        for(int q=0; q<i; q++){
         if(q==0){
           b(i, j, q) = a(i, j);
         }
         else{
           b(i, j, q) = 0.0;
         }
      }
    }
  }
  return b;
}

-- 
*Amina Shahzadi*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160927/061fb7d6/attachment.html>


More information about the Rcpp-devel mailing list