I am new to this list, so I hope this is the right place to ask this question. I am trying to translate the R vectorized loop below to C++ in order to speed up my calculations:

Let:
> n1
 [1] 1 1 2 2 2 3 3 4 4 4 5 5 5 5 6 6 6 7 7 8 8 8 9 9
> n2
 [1] 2 4 1 3 5 2 6 1 5 7 2 4 6 8 3 5 9 4 8 5 7 9 6 8
> w1w1
 [1] 0.2500000 0.2500000 0.1111111 0.1111111 0.1111111 0.2500000 0.2500000
 [8] 0.1111111 0.1111111 0.1111111 0.0625000 0.0625000 0.0625000 0.0625000
[15] 0.1111111 0.1111111 0.1111111 0.2500000 0.2500000 0.1111111 0.1111111
[22] 0.1111111 0.2500000 0.2500000

My vectorized loop is:

    tWSWS.k <- lapply(c(1:length(n1)), function(.n1){
      lapply(c(1:length(n2)), function(.n2){
        if(.n1!=.n2){
         w1w1[n1==.n1 & n2==.n2]
       }})})

result=sum(unlist(tWSWS.k))

Could you help me with this translation or at least point me out to some reference/example?

Thanks!

Nelson

--
Nelson Villoria
Research Assistant Professor
Center for Global Trade Analysis
Department of Agricultural Economics
Purdue University
403 W. State St
West Lafayette IN 47906
765.494.4303
nvill...@purdue.edu

_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to