我试图写下计算每行矩阵之间的最大距离的距离函数,但是,在计算最大值之前,我需要删除某些元素,问题发布在代码中。
我怎样才能以有效的方式完成它?
#include <Rcpp.h>
Rcpp::NumericMatrix Mydist (const Rcpp::NumericMatrix & x){
unsigned int ans = x.nrow(), i = 0, j = 0;
double d;
Rcpp::NumericMatrix out(ans,ans);
for (i = 0; i < ans - 1; i++){
Rcpp::NumericVector temp = x.row(i);
for (j = i + 1; j < ans ; j ++){
#
# here, I need to remove the i and j element in the vector temp-x.row(j) before I can do
# the abs() and max(), but i dont know
# how to do it efficiently?
#
d = max(abs(temp-x.row(j)));
#
out(j,i)=d;
out(i,j)=d;
}
}
return out;
}
在 R 中,对于部分d = max(abs(temp-x.row(j)));
,我想要的等效 R 代码是
Asuume there is a vector called tem = temp-x.row(j)
d = max(abs(tem[-c(i,j)]))