[Rcpp-devel] Trailing Index issues, handled as pointer
Mario Deng
mariodeng at googlemail.com
Mon Jul 7 17:17:26 CEST 2014
Thanks alot,
solved my problem within seconds. But why is the result from match() 1-indexed? I thought it would be a native cpp function and expected a 0-based index.
__
MD
mariodeng at googlemail.com
On 07.07.2014, at 16:59, John Laing <john.laing at gmail.com> wrote:
> Mario,
>
> Your vector matched is 1-indexed, but by passing an element of this vector (match_id) to rownames[] you're treating it as if it's 0-indexed.
>
> -John
>
>
>
>
>
> On Mon, Jul 7, 2014 at 10:33 AM, Mario Deng <mariodeng at googlemail.com> wrote:
> Hello everyone,
>
> I am trying to implement a data structure using Rcpp. But I am already struggling with my first method.
> How things (should) work: I want to my function ("createVectorList") a data frame or matrix. Each column of the matrix gets sorted. This also holds for the rownames - storing the sorted rownames for each sort column gives me big memory footprint, but log(n) access while reading later.
> Basically everything is fine, but when accessing some data through a for loop I get "address 0x0, cause 'memory not mapped'".
>
> Here is my Code:
>
> #include <Rcpp.h>
> using namespace Rcpp;
>
> // [[Rcpp::export]]
> List createVectorList(NumericMatrix df, CharacterVector rownames) {
> List sorted_rownames(df.ncol());
> for(int i = 0; i < df.ncol(); i++){
> NumericVector sorted_vec = df(_,i);
> std::sort(sorted_vec.begin(), sorted_vec.end());
> NumericVector one_col = df(_,i);
> df(_,i)=sorted_vec;
> IntegerVector matched = match( sorted_vec, one_col);
> CharacterVector one_sorted_row(df.nrow());
> for(int j = 0; j < one_sorted_row.length(); j++){
> int match_id=matched[j];
> String r_name=rownames[match_id];
> one_sorted_row[j]=r_name;
> }
> sorted_rownames[i]=one_sorted_row;
> }
> return Rcpp::List::create(Rcpp::Named("Matrix") = df, Rcpp::Named("rowIDX")=sorted_rownames);
> }
>
> There trouble is within these lines:
>
> for(int j = 0; j < one_sorted_row.length(); j++){
> int match_id=matched[j];
> String r_name=rownames[match_id];
> one_sorted_row[j]=r_name;
> }
>
> When executing this, I get the error named above.
> What I thought about: rownames should be of the type "Proxy" or so, maybe thats the point?
>
> Maybe one of you has a hint.
>
> With beste regards,
>
> Mario
> __
> MD
> mariodeng at googlemail.com
>
>
>
>
> _______________________________________________
> 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