[Rcppdevel] subsetting a matrix
Dirk Eddelbuettel
edd at debian.org
Tue Mar 4 03:41:01 CET 2014
On 3 March 2014 at 20:13, Yixuan Qiu wrote:
 Hi Hideyoshi,
 You may try the code below:


 cppFunction(depends = "RcppArmadillo",
 'NumericMatrix ss(NumericMatrix X_, IntegerVector ind_) {

 int n = X_.nrow(), k = X_.ncol();

 arma::mat X(X_.begin(), n, k, false);
 arma::uvec ind = as<arma::uvec>(ind_);
 arma::mat submat = X.rows(ind  1);

 return wrap(submat);
 }')

 m = matrix(rnorm(90), ncol=3);
 ss(m, c(1, 5, 12, 22))
Nice.
Also see
http://gallery.rcpp.org/articles/armadillosubsetting/
Dirk



 Best,
 Yixuan


 20140303 19:24 GMT05:00 Hideyoshi Maeda <hideyoshi.maeda at gmail.com>:

 Hi Rcppdevel list,

 I would like to subset a matrix by only selecting certain rows.

 Something like the below?where y is a vector of integers that represent the
 rows to be selected. However the below does not seem to work.

 // [[Rcpp::export]]
 Rcpp::NumericMatrix ss(SEXP X_,SEXP y_){
 Rcpp::NumericMatrix X(X_)
 Rcpp::NumericVector y(y_)
 return X[y];
 }

 R> ss(matrix(rnorm(90),ncol=3),c(1,5,12,22))

 It is a bit similar to this question http://stackoverflow.com/questions/
 13038256/subsetofarcppmatrixthatmatchesalogicalstatement however
 rather than just a single value, the find it for a vector.

 also I am not too sure to what extent the Armadillo submat function is able
 to say subset for example rows: 1,5, 12 and 22 and then call the resulting
 matrix something else?

 I was hoping that this subsetting procedure was going to be easy?but I seem
 to have got stuck.

 thanks

 HLM


 _______________________________________________
 Rcppdevel mailing list
 Rcppdevel at lists.rforge.rproject.org
 https://lists.rforge.rproject.org/cgibin/mailman/listinfo/rcppdevel




 
 Yixuan Qiu <yixuan.qiu at cos.name>
 Department of Statistics,
 Purdue University

 
 _______________________________________________
 Rcppdevel mailing list
 Rcppdevel at lists.rforge.rproject.org
 https://lists.rforge.rproject.org/cgibin/mailman/listinfo/rcppdevel

Dirk Eddelbuettel  edd at debian.org  http://dirk.eddelbuettel.com
More information about the Rcppdevel
mailing list