[Rcpp-devel] Easy way to subset a matrix
Zarrar Shehzad
zarrar.shehzad at yale.edu
Wed Sep 21 07:09:39 CEST 2011
I am not sure if there are native functions in Rcpp but you could use
RcppArmadillo to solve your problem.
So say Xs = x:
// Convert from SEXP => Rcpp => Arma
Rcpp::NumericMatrix Xr(Xs);
arma::mat X(Xr.begin(), Xr.nrow(), Xr.ncol(), false);
// Get subset of matrix and calculate variance
// (i.e., do var(x[3:10,5]))
arma::var( X.submat(3, 10, 5, 5) );
The armadillo docs are really nice so you can see those for more info on the
var and submat functions.
Cheers
Zarrar
On Wed, Sep 21, 2011 at 12:16 AM, Noah Silverman <noahsilverman at ucla.edu>wrote:
> Hello,
>
> I want to calculate the variance of a subset of a matrix column.
>
> For example, if I wanted the variance of items 3-10 in column 5.
>
> In R, this would be:
>
> x <- matrix(rnorm(100), nrow=10, ncol=10)
> varx <- var(x[3:10,5])
>
> In Rcpp, I can construct a matrix object Rcpp::NumericMatrix x
> The var function is available thanks to the great Sugar implementation
>
> But, how can I easily reference a subset of a column to calculate the
> variance?
>
> Ideas?
>
>
> --
> Noah Silverman
> UCLA Department of Statistics
> 8117 Math Sciences Building #8208
> Los Angeles, CA 90095
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110921/5a53d4fa/attachment.htm>
More information about the Rcpp-devel
mailing list