Is there a way to create NumericMatrix from NumericVectors? Something like this:
Rcpp::cppFunction("NumericMatrix f(){
NumericVector A(10, 2.0);
NumericVector B(10, 1.0);
return NumericMatrix(10,2,C(A,B)); //ERROR
}")
> f()
Sure. There is for example cbind
.
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericMatrix makeMatrix(Rcpp::NumericVector a, Rcpp::NumericVector b) {
return Rcpp::cbind(a, b);
}
/*** R
a <- c(1,2,3)
b <- c(3,2,1)
makeMatrix(a,b)
*/
> Rcpp::sourceCpp("~/git/stackoverflow/65538515/answer.cpp")
> a <- c(1,2,3)
> b <- c(3,2,1)
> makeMatrix(a,b)
[,1] [,2]
[1,] 1 3
[2,] 2 2
[3,] 3 1
>