实施For cases involving double, e.g. arma::mat/arma::vec/ et cetera, consider using find_finite()#include <RcppArmadillo.h>// [[Rcpp::depends(RcppArmadillo)]]// [[Rcpp::export]]arma::mat get_mw_mean_na(arma::imat x){ int num_r = x.n_rows - 1; int num_c = x.n_cols - 1; Rcpp::Rcout << x <<std::endl; arma::dmat result(num_r, num_c); for (int i = 0; i < num_r; i++) { for (int j = 0; j < num_c; j++) { arma::imat sub_x = x.submat(i, j, i + 1, j + 1); // Conversion + Search for NA values arma::vec sub_x_v2 = arma::conv_to<arma::vec>::from( sub_x.elem( find(sub_x != INT_MIN) ) ); result(i, j) = arma::mean(sub_x_v2); } } return result;}输出new_c1 = c(1, 86, 98, 15, 5, 85, 32, 25, 68)lg1 = matrix(new_c1, nrow = 3, byrow = TRUE)get_mw_mean_na(lg1)# [,1] [,2]# [1,] 26.75 68.50# [2,] 19.25 45.75new_c2 = c(NA, 86, 98, 15, NA, 85, 32, 25, 68)lg2 = matrix(new_c2, nrow = 3, byrow = TRUE)get_mw_mean_na(lg2)# [,1] [,2]# [1,] 50.5 89.66667# [2,] 24.0 59.33333 这篇关于rcpp:在移动窗口计算中删除NAs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-26 15:02