cbind在R中,在重复调用中相对耗时,但它对于各种数据类型也是强大的.我编写的代码比cbind绑定两个矩阵快3倍.但bind_cols在dplyr包装仅仅是100X的速度比cbind.遗憾的是它不能将矩阵作为输入.有人可以使下面的代码更快.另外,如何快速绑定稀疏矩阵?这是我使用的代码:
require( Rcpp )
func <- 'NumericMatrix mmult(NumericMatrix a,NumericMatrix b) {
//the colnumber of first matrix
int acoln=a.ncol();
//the colnumber of second matrix
int bcoln=b.ncol();
//build a new matrix, the dim is a.nrow() and acoln+bcoln
NumericMatrix out(a.nrow(),acoln+bcoln) ;
for (int j = 0; j < acoln + bcoln; j++) {
if (j < acoln) {
out(_,j) = a(_,j);
} else {
//put the context in the second matrix to the new …Run Code Online (Sandbox Code Playgroud)