Rcpp: rearrange a vector in an order of another vector

无人久伴 提交于 2019-12-22 00:17:25

问题


I am new in Rcpp. I need to rearrange a vector A in an order of another vector B; For example,

A=c(0.5,0.4,0.2,0.9)
B=c(9,1,3,5)

I want to make C=c(0.4,0.2,0.9,0.5) by Rcpp.

I know the simple r code, C=A[order(B)], but it is necessary for me to use Rcpp code.

I found how to find the order of B by using sort_index, but I fail to arrange A with respect to an order of B.

How can I make it?


回答1:


You should be able to use arma::sort_index for this, which you mention in your post:

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
arma::vec arma_sort(arma::vec x, arma::vec y) {
    return x(arma::sort_index(y));
}

/*** R
A <- c(0.5, 0.4, 0.2, 0.9)
B <- c(9, 1, 3, 5)
arma_sort(A, B)
*/

Result:

> arma_sort(A, B)
     [,1]
[1,]  0.4
[2,]  0.2
[3,]  0.9
[4,]  0.5

Of course, there are other ways as well. Variations on this question have been asked a few times on Stack Overflow in the context of plain C++. Below I've adapted the answer here for Rcpp:

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
NumericVector Rcpp_sort(NumericVector x, NumericVector y) {
    // Order the elements of x by sorting y
    // First create a vector of indices
    IntegerVector idx = seq_along(x) - 1;
    // Then sort that vector by the values of y
    std::sort(idx.begin(), idx.end(), [&](int i, int j){return y[i] < y[j];});
    // And return x in that order
    return x[idx];
}

/*** R
A <- c(0.5, 0.4, 0.2, 0.9)
B <- c(9, 1, 3, 5)
Rcpp_sort(A, B)
*/

Result:

> Rcpp_sort(A, B)
[1] 0.4 0.2 0.9 0.5


来源:https://stackoverflow.com/questions/48118248/rcpp-rearrange-a-vector-in-an-order-of-another-vector

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!