[Rcpp-devel] filling in blocks of a matrix

Hideyoshi Maeda hideyoshi.maeda at gmail.com
Thu Feb 13 12:23:42 CET 2014


Dear Rcpp-List,

I am looking to see it is possible to fill in a matrix with another matrix.

I have three matrices, out1 which is a 5x5 zero matrix

out2 is a 3x3 matrix with each value set as 8

and out3 which is a 5x4 matrix with each value set as 9

I would like to change part of my out1 matrix by filling it with the values in out2 or out3, is this possible? 

Would I just need to use loops to sequentially fill it in (either row by row or column by column etc)? or could I just assign new values to the bits i want to change?

Below is my attempt at doing the later, but it does not compile saying that for this line "out1(Range(0,2),Range(1,3)) = out2;” there is no viable overload ‘='

Perhaps this is rather simple but I am very new to Rcpp, but any help would be much appreciated.

Regards

HLM

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
Rcpp::NumericMatrix f1(String h){
  String h1 = h;

  NumericMatrix out1(5,5);
  NumericMatrix out2(3,3);
  NumericMatrix out3(5,4);
  std::fill(out2.begin(), out2.end(), 8);
  std::fill(out3.begin(), out2.end(), 9);

  if(h1=="nc"){
    out1(Range(0,2),Range(1,3)) = out2;
  } else {
    out1(_,Range(1,4)) = out3;
  }
  return out1;
}


More information about the Rcpp-devel mailing list