[Rcpp-devel] how to reference a row of a matrix in C++ in Rcpp
akshay kulkarni
akshay_e4 at hotmail.com
Wed Mar 21 09:40:42 CET 2018
dear serguei,
I have another question:
If M is a matrix, M.nrow() should return the number of rows...but I encountered the following inconsistency:
> M
x y z
[1,] 1 0 0
[2,] 4 3 0
[3,] 3 1 1
[4,] 2 90 87
[5,] 9 76 23
[6,] 5 23 13
> cppFunction('IntegerVector tccp5(IntegerMatrix M) { int x = M.nrow(); return x;}')
> tccp5(M)
[1] 0 0 0 0 0 0
can you please explain what is happening( I am very new to Rcpp and C++)?
very many thanks for your time and effort...
yours sincerely,
AKSHAY M KULKARNI
________________________________
From: Serguei Sokol <serguei.sokol at gmail.com>
Sent: Tuesday, March 20, 2018 7:26 PM
To: akshay kulkarni; Rcpp R
Subject: Re: [Rcpp-devel] how to reference a row of a matrix in C++ in Rcpp
Le 20/03/2018 à 12:10, akshay kulkarni a écrit :
> dear members,
>
> I came to know from stackoverflow that the following references a row in a matrix in C++:
>
> M[2] references 2nd row of the Matrix.
SO is too big to check this assertion by ourself. Do you have a link?
>
>
> I am using Rcpp to write C++ code in R.
>
>
> However, I ended up with the following inconsistency:
>
> > M
> x y z
> [1,] 1 1 1
> [2,] 2 2 2
> [3,] 3 3 3
> [4,] 4 4 4
> [5,] 5 5 5
> [6,] 6 6 6
> > cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M[2]; return x;}')
Try
cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M(2,_); return x;}')
Best,
Serguei.
> > tccp3(M)
> [1] 0 0 0
> > cppFunction('IntegerVector tccp4(IntegerMatrix M) { IntegerVector x = M[1]; return x;}')
> > tccp4(M)
> [1] 0 0
>
> tccp3 should return (3,3,3) and tccp4 should return (2,2,2). Can you please shed light on what is going on?
>
> very many thanks for your time and effort....
>
> Yours sincerely,
> AKSHAY M KULKARNI
>
>
>
> _______________________________________________
> 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
Rcpp-devel Info Page<https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel>
lists.r-forge.r-project.org
Discussion list for Rcpp, RInside and various packages using Rcpp or RInside. This list is subscriber-only, in other words in order to post to the list you must be ...
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20180321/7b44a29a/attachment-0001.html>
More information about the Rcpp-devel
mailing list