[Rcpp-devel] Most efficient way to apply function over rows of a matrix with Rcpp?

Kevin Ushey kevinushey at gmail.com
Fri Jan 18 03:09:52 CET 2013


Hi guys,

I'm trying to use Rcpp to implement an 'apply'-type function for matrices,
whereby I apply a function to each row or column of a matrix. I'm testing
it here with the 'mean' just to see how well we can do vs. the highly
optimized rowMeans, colMeans functions, but of course any other function
taking a vector and returning a scalar might fit here.

I'm wondering if I can do better. Please see the gist in the link here; you
can sourceCpp it in R.

https://gist.github.com/4561281

I try to limit the amount of copying as much as possible with
NumericMatrix::Column and NumericMatrix::Row.

Note that the Rcpp solution over columns is just as fast as colMeans, but
over rows it's a fair bit slower relative to rowMeans. Is there any way I
could improve this?

I plan to submit an expanded version of this to the Rcpp gallery so any
advice is appreciated!

Thanks,
-Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130117/6a3d2b6a/attachment.html>


More information about the Rcpp-devel mailing list