Skip to content

[Rcpp-devel] Non-contiguous submatrix

5 messages · Romain Francois, Simon Zehnder, Dirk Eddelbuettel

#
Dear Rcpp::Users and Rcpp::Devels,

I am right now working on a relabeling algorithm where I have a function taking a permutation matrix of all permutations of the labels. Inside the function the values should be permuted in respect to a row of this matrix. In R I would use the following commands:

values <- matrix(rnorm(4), ncol = 2, nrow = 2, byrow = TRUE)
perm <- as.matrix(expand.grid(seq(1, 2), seq(1, 2)))
index <- perm[which(perm[, 1] != perm[, 2]),]
values[, index[, 1]]
values[, index[, 2]]

I found this thread on stackoverflow: http://stackoverflow.com/questions/8435169/selecting-a-non-contiguous-submatrix-in-rcpp. Are there any proceedings for non-contiguous submatrices in Rcpp? I couldn't find a hint in the Rcpp-book (neither in chapter 3 nor in chapter 8). I would like to avoid nested loops and I need the values as Rcpp::NumericMatrix objects for the functions (so using RcppArmadillo (via non-contiguous views) is only a path I would choose if no other simpler possibilities are available). Any suggestions are welcome. 

@Rcpp::Devels: I try to understand in the doxygen documentation how the operator '()' works. I see that there is always a VECTOR::Proxy underlying each Matrix object and I end up at the traits::r_vector_proxy in the 00_forward_proxy.h file. I assume, that each Matrix object has its type given by the proxy and acts in regard to this characterization. Where can I look for more information about this struct? 

Best

Simon
#
Le 26 ao?t 2013 ? 12:19, Simon Zehnder <szehnder at uni-bonn.de> a ?crit :
Writing loops is what you do in c++. There is no guilty conscience like in R.
In the source files. You'll have to jump between several files. Not easy to follow, even for us. Good luck.
#
Hi Romain,

thanks for the quick reply! This is an answer I can work with!

Best

Simon
On Aug 26, 2013, at 12:33 PM, Romain Francois <romain at r-enthusiasts.com> wrote:

            
#
On 26 August 2013 at 12:19, Simon Zehnder wrote:
| Dear Rcpp::Users and Rcpp::Devels,
| 
| I am right now working on a relabeling algorithm where I have a function
| taking a permutation matrix of all permutations of the labels. Inside the
| function the values should be permuted in respect to a row of this
| matrix. In R I would use the following commands:
| 
| values <- matrix(rnorm(4), ncol = 2, nrow = 2, byrow = TRUE)
| perm <- as.matrix(expand.grid(seq(1, 2), seq(1, 2)))
| index <- perm[which(perm[, 1] != perm[, 2]),]
| values[, index[, 1]]
| values[, index[, 2]]
| 
| I found this thread on stackoverflow:
| http://stackoverflow.com/questions/8435169/selecting-a-non-contiguous-submatrix-in-rcpp. Are
| there any proceedings for non-contiguous submatrices in Rcpp?

Not in Rcpp -- but Armadillo has it and this example on the Rcpp Gallery uses
it along with RcppArmadillo:

  http://gallery.rcpp.org/articles/armadillo-subsetting/

Dirk
#
Thanks Dirk! That looks interesting! I will check which solution performs better in my application! 

Best

Simon
On Aug 26, 2013, at 1:24 PM, Dirk Eddelbuettel <edd at debian.org> wrote: