[Rcpp-devel] subsetting a matrix

Kevin Ushey kevinushey at gmail.com
Tue Mar 4 02:52:16 CET 2014


If you want more generic matrix operations, it really is best to use
Armadillo (as per Yixuan's example; note that the constructor he's
using reuses R's memory as well, so it's efficient -- no copy is made,
that object can be modified in place).

However, you could always do something like the following:

#include <Rcpp.h>
using namespace Rcpp;

class MyMatrix : public NumericMatrix {

public:

    MyMatrix(NumericMatrix x): NumericMatrix(x) {};

    // row subsetting
    NumericMatrix rows(IntegerVector x) {

        int nrow = this->nrow();
        int ncol = this->ncol();
        int nx = x.size();

        NumericMatrix output(nx, ncol);
        int counter = 0;
        for (int i=0; i < ncol; ++i) {
            for (int j=0; j < nx; ++j) {
                output[counter++] = (*this)[ x[j] + i * nrow ];
            }
        }

        return output;
    }

};

Cheers,
Kevin

On Mon, Mar 3, 2014 at 5:13 PM, Yixuan Qiu <yixuan.qiu at cos.name> 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))
>
>
>
> Best,
> Yixuan
>
>
> 2014-03-03 19:24 GMT-05:00 Hideyoshi Maeda <hideyoshi.maeda at gmail.com>:
>>
>> Hi Rcpp-devel 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/subset-of-a-rcpp-matrix-that-matches-a-logical-statement
>> 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
>>
>>
>> _______________________________________________
>> 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
>
>
>
>
> --
> Yixuan Qiu <yixuan.qiu at cos.name>
> Department of Statistics,
> Purdue University
>
> _______________________________________________
> 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


More information about the Rcpp-devel mailing list