# [Rcpp-devel] Use of SubMatrix function

Søren Højsgaard sorenh at math.aau.dk
Sat Apr 21 15:46:52 CEST 2012

```Dear list

I've tried to write a function for extracting a submatrix i.e. for mimicing A[c(1,3,4),c(2,3,5)]. So I did:

submat <- cxxfunction(signature(r_SS='int', r_aa='int', r_bb='int'), body = '
NumericMatrix SS (r_SS);
IntegerVector aa (r_aa);
IntegerVector bb (r_bb);
SubMatrix<INTSXP> yy = SS( aa, bb ) ;
', plugin='Rcpp')

submat(matrix(1:9,nr=3), 0:1, 1:2)

This fails with the error message:
....
31: NumericMatrix SS (r_SS);
32: IntegerVector aa (r_aa);
33: IntegerVector bb (r_bb);
34: SubMatrix<INTSXP> yy = SS( aa, bb ) ;
35:
36: END_RCPP
37: }
38:
39:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! file1e00bc456c.cpp: In function 'SEXPREC* file1e00bc456c(SEXP, SEXP, SEXP)':
file1e00bc456c.cpp:34:35: error: invalid conversion from 'SEXP' to 'size_t {aka unsigned int}' [-fpermissive]
C:/programs/R/devel/library/Rcpp/include/Rcpp/vector/Matrix.h:126:18: error:   initializing argument 1 of 'Rcpp::Matrix<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&, const size_t&) [with int RTYPE = 14, Rcpp::Matrix<RTYPE>::Proxy = double&, size_t = unsigned int]' [-fpermissive]
file1e00bc456c.cpp:34:35: error: invalid conversion from 'SEXP' to 'size_t {aka unsigned int}' [-fpermissive]
C:/programs/R/devel/library/Rcpp/include/Rcpp/vector/Matrix.h:126:18: error:   initializing argument 2 of 'Rcpp::Matrix<RTYPE>::Proxy Rcpp::Matrix<RTYPE>::operator()(const size_t&, const size_t&) [with int RTYPE = 14, Rcpp::Matrix<RTYPE>::Proxy = double&, size_t = unsigned int]' [-fpermissive]
file1e00bc456c.cpp:34:35: error: conversion from
running command 'C:/programs/R/devel/bin/i386/R CMD SHLIB file1e00bc456c.cpp 2> file1e00bc456c.cpp.err.txt' had status 1

I've seen examples where SubMatrix has been used in connection with e.g. SS(Range(0,1), Range(2,4)) and that works. From this one can not conclude that it should work with arbitrary index vectors (aa and bb in my example) - I know that. But what puzzles me is the error message above:
invalid conversion from 'SEXP' to 'size_t {aka unsigned int}' [-fpermissive]
I read this such that aa is really an SEXP. However, my understanding was that
IntegerVector aa (r_aa);
would coerce an SEXP (here r_aa) to an integer vector.

Can anyone spare a moment to clarify this for me?

Best regards
Søren

```