[Rcpp-devel] Issue with Matrices and Iterators
Alessandro Mammana
mammana at molgen.mpg.de
Mon Oct 14 21:54:14 CEST 2013
You are right, now the code compiles! But why?
I understand that data is stored in column major. However I hoped that
using an iterator I would abstract away from how the data is stored
and that the function that increases the iterator takes care of
increasing the pointer of nrow positions...
Thanks again!
Ale
On Mon, Oct 14, 2013 at 9:39 PM, Matteo Fasiolo
<matteo.fasiolo at gmail.com> wrote:
> Hi Ale,
>
> maybe I'm wrong but mat(0,_) is not a NumericVector, but a Rcpp::MatrixRow.
> So your function sum() is being called with arguments of the wront type (the
> error
> message is saying that).
>
> If instead you select a column mat( _, 0) you have a conversion to a
> NumericVector.
> I've encountered the same issue a couple of time.
>
> Matteo
>
>
> On Mon, Oct 14, 2013 at 8:21 PM, Alessandro Mammana <mammana at molgen.mpg.de>
> wrote:
>>
>> Hi all,
>> I have very little experience with Rcpp and also with c++, so I am
>> sorry if my question might be too stupid.
>> I am trying to use iterators in my code, because I understand them
>> better than vectors, because they should be efficient, and because I
>> should be able to combine different implementations of vector and
>> matrix classes. However the following code does not compile:
>>
>> #include <Rcpp.h>
>> using namespace Rcpp;
>>
>> typedef NumericVector::iterator DoubleIter;
>>
>> double sum(DoubleIter b, DoubleIter e){
>> double sum = 0;
>> while (b!=e){
>> sum += *(b++);
>> }
>> return sum;
>> }
>>
>> // [[Rcpp::export]]
>> double sumFirstRow(NumericMatrix mat){
>> return sum(mat(0,_).begin(), mat(0,_).end());
>> }
>>
>> The error message is not very useful (or at least I cannot interpret it):
>>
>> In function 'double sumFirstRow(Rcpp::NumericMatrix)':
>> error: no matching function for call to
>> 'sum(Rcpp::MatrixRow<14>::iterator, Rcpp::MatrixRow<14>::iterator)'
>> note: candidates are:
>> note: double sum(DoubleIter, DoubleIter)
>> note: no known conversion for argument 1 from
>> 'Rcpp::MatrixRow<14>::iterator' to 'DoubleIter {aka double*}'
>>
>> Am I missing something very basic?
>> Thanks a lot!
>> Ale
>> _______________________________________________
>> 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