Ich würde entscheiden RcppArmadillo über RCPP verwenden wie es robuste Matrix Manipulation hat. Zum Beispiel können Sie die index_min()
or index_max()
einer Teilmenge schnell finden und dann in die tiefgestellte Notation mit ind2sub()
übersetzen. Eine Sache zu beachten ist C++ verwendet 0-basierte Indizes und R verwendet 1-basierte Indizes so sollten Sie sicherstellen, hinzuzufügen, wenn das Ziel, den Index Index in R zu verwenden ist.
Die folgende Beschreibung ist für Ihren Fall arbeiten:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::urowvec get_min_cols(const arma::mat& x, const arma::uvec& col_indexes) {
// Obtain the minimum index from the selected columns
arma::uword min_index = x.cols(col_indexes).index_min();
// Obtain the subscript notation from index based on reduced dimensions
arma::uvec min_subnot = arma::ind2sub(arma::size(x.n_rows, col_indexes.n_elem),
min_index);
// Transpose to row vector and
// translate indices to _R_ from _C++_ by adding 1
return min_subnot.t() + 1;
}
Test:
set.seed(1984)
m = matrix(sample.int(25,25), 5)
col_indexes = c(1, 3)
min_loc_r = which(m[, col_indexes] == min(m[, col_indexes]), arr.ind = TRUE)
# Note that the column indices has been translated to C++
min_loc_cpp = get_min_cols(m, col_indexes - 1)
min_loc_r
# row col
# [1,] 5 2
min_loc_cpp
# [,1] [,2]
# [1,] 5 2
http://idownvotedbecau.se/noattempt/ – Murphy
Warum ihn Downvoting? Vielleicht nur erklären und ihm helfen ... Sie laufen R Community –