如何解决删除几个元素后的Rcpp最大值
我试图写下距离函数,该函数计算矩阵的每一行之间的最大距离,但是,在计算最大值之前,我需要删除某些元素,问题已发布在代码中。
如何有效地完成它?
#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)]))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。