Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6905789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:13:38+00:00 2026-05-27T08:13:38+00:00

I am trying to select a submatrix in Rcpp with non-contiguous slices. The equivalent

  • 0

I am trying to select a submatrix in Rcpp with non-contiguous slices. The equivalent R code is

> xx = matrix(0,nrow=10,ncol=8)
> xx[,c(1,3,4)]
      [,1] [,2] [,3]
 [1,]    0    0    0
 [2,]    0    0    0
 [3,]    0    0    0
 [4,]    0    0    0
 [5,]    0    0    0
 [6,]    0    0    0
 [7,]    0    0    0
 [8,]    0    0    0
 [9,]    0    0    0
[10,]    0    0    0

In Rcpp, I am tried to do

Rcpp::NumericMatrix xx(10,8);
Rcpp::NumericMatrix aa = xx(Rcpp::Range(0,9), Rcpp::NumericVector::create(1,3,4));

However, this gives

error: no match for call to '(Rcpp::NumericMatrix) (Rcpp::Range, Rcpp::Vector<14>)'
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:126: note: candidates are: typename Rcpp::Vector<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&, const size_t&) [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:132: note:                 typename Rcpp::Vector<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&, const size_t&) const [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:142: note:                 Rcpp::MatrixRow<RTYPE> Rcpp::Matrix<RTYPE>::operator()(int, Rcpp::internal::NamedPlaceHolder) [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:146: note:                 Rcpp::MatrixColumn<RTYPE> Rcpp::Matrix<RTYPE>::operator()(Rcpp::internal::NamedPlaceHolder, int) [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:150: note:                 Rcpp::SubMatrix<RTYPE> Rcpp::Matrix<RTYPE>::operator()(const Rcpp::Range&, const Rcpp::Range&) [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:154: note:                 Rcpp::SubMatrix<RTYPE> Rcpp::Matrix<RTYPE>::operator()(Rcpp::internal::NamedPlaceHolder, const Rcpp::Range&) [with int RTYPE = 14]
/opt/local/lib/R/library/Rcpp/include/Rcpp/vector/Matrix.h:158: note:                 Rcpp::SubMatrix<RTYPE> Rcpp::Matrix<RTYPE>::operator()(const Rcpp::Range&, Rcpp::internal::NamedPlaceHolder) [with int RTYPE = 14]

Is this possible in Rcpp?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:13:38+00:00Added an answer on May 27, 2026 at 8:13 am

    Here’s how I’ve done it:

    library(inline)
    
    testSlice = cxxfunction(signature(r_mat='int', r_cols='int'), body = 
    '
    NumericMatrix mat (r_mat);
    NumericVector cols (r_cols);
    
    NumericMatrix matslice (mat.nrow(), cols.size());
    
    for (int i=0; i<cols.size(); i++) {
      matslice(_,i) = mat(_, cols(i)-1);
    }
    
    return(matslice);
    '
    , plugin='Rcpp')
    
    > xx = matrix(sample(10, 10*10, replace=T), nrow=10)
    > xx
          [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
     [1,]    6    7    9    8    2    6    1    8   10     8
     [2,]    5    6    9    5    1    5    2    7   10     3
     [3,]    9    5    1   10    8   10   10    1    2     7
     [4,]    2    7    1    4    5    1    8    2   10     9
     [5,]    6    6    9    8    3    1    2   10    2     1
     [6,]    4    6    7    9    7    5    4    7   10    10
     [7,]   10   10    8    8   10    7   10    4    6     3
     [8,]    6    3   10    6    3    2    5    4    1    10
     [9,]    8    7    2    7    9    7    7    8    3    10
    [10,]    5    4    5    4    4    8   10    1    5     4
    
    > testSlice(xx, c(2,5,6))
          [,1] [,2] [,3]
     [1,]    7    2    6
     [2,]    6    1    5
     [3,]    5    8   10
     [4,]    7    5    1
     [5,]    6    3    1
     [6,]    6    7    5
     [7,]   10   10    7
     [8,]    3    3    2
     [9,]    7    9    7
    [10,]    4    4    8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying select a field of my date base, the code is: if (db.db().isOpen())
Im trying to select a timestamp range from a mysql database and im having
I'm trying to select a document by id I've tried: collection.update({ _id: { $oid:
I am trying to SELECT a time range while ignoring the date portion of
In trying to select elements that have any attributes, the following throws a jQuery
When trying to select fields from a subquery if ANY of the subqueries do
I'm trying to select all <div> s with the same ID in jQuery. How
I'm trying to select orders that have either over or under 2000 products ordered
I'm trying to select some integer values in MySQL. Several of the values are
I'm trying to SELECT based on a date in OpenOffice Base: SELECT * FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.