Skip to content

Instantly share code, notes, and snippets.

@gghatano
Last active August 29, 2015 14:02
Show Gist options
  • Save gghatano/623e8e3b2ee5c5023eba to your computer and use it in GitHub Desktop.
Save gghatano/623e8e3b2ee5c5023eba to your computer and use it in GitHub Desktop.
Rcppで行列処理
library(Rcpp)
sourceCpp("timesTwo.cpp")
x = matrix(1:9, nrow = 3, ncol= 3)
timesTwo(x)
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix timesTwo(NumericMatrix x) {
NumericMatrix y(3,3);
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
y(i,j) = 2*x(i,j);
}
}
return y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment