<div dir="ltr"><div><div><div>Hi Hideyoshi,<br></div>You may try the code below:<br><br><br><span style="font-family:courier new,monospace">cppFunction(depends = "RcppArmadillo",<br>        'NumericMatrix ss(NumericMatrix X_, IntegerVector ind_) {<br>
             <br>             int n = X_.nrow(), k = X_.ncol();<br>             <br>             arma::mat X(X_.begin(), n, k, false);<br>             arma::uvec ind = as<arma::uvec>(ind_);<br>             arma::mat submat = X.rows(ind - 1);<br>
             <br>             return wrap(submat);<br>         }')<br><br>m = matrix(rnorm(90), ncol=3);<br>ss(m, c(1, 5, 12, 22))</span><br><br><br><br></div>Best,<br></div>Yixuan<br></div><div class="gmail_extra"><br>
<br><div class="gmail_quote">2014-03-03 19:24 GMT-05:00 Hideyoshi Maeda <span dir="ltr"><<a href="mailto:hideyoshi.maeda@gmail.com" target="_blank">hideyoshi.maeda@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word">Hi Rcpp-devel list,<div><br></div><div>I would like to subset a matrix by only selecting certain rows.</div><div><br></div><div>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.</div>
<div><br></div><div><div>// [[Rcpp::export]]</div><div>Rcpp::NumericMatrix ss(SEXP X_,SEXP y_){</div><div>  Rcpp::NumericMatrix X(X_)</div><div>  Rcpp::NumericVector y(y_)</div><div>  return X[y];</div><div>}</div><div><br>
</div><div>R> ss(matrix(rnorm(90),ncol=3),c(1,5,12,22))</div><div><br></div></div><div>It is a bit similar to this question <a href="http://stackoverflow.com/questions/13038256/subset-of-a-rcpp-matrix-that-matches-a-logical-statement" target="_blank">http://stackoverflow.com/questions/13038256/subset-of-a-rcpp-matrix-that-matches-a-logical-statement</a> however rather than just a single value, the find it for a vector.</div>
<div><br></div><div>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?</div><div><br></div><div>I was hoping that this subsetting procedure was going to be easy…but I seem to have got stuck.</div>
<div><br></div><div>thanks</div><div><br></div><div>HLM</div><div><br></div></div><br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br><br clear="all"><br>
-- <br>Yixuan Qiu <<a href="mailto:yixuan.qiu@cos.name" target="_blank">yixuan.qiu@cos.name</a>><br>Department of Statistics,<br>Purdue University<br>
</div>