Reputation: 545
I'm trying to write down the distance function which calculate the maximum distance between each rows of matrix, however, before calculate the maximum value, I need to remove certain element, the question is posted in the code.
How can I accomplish it in an efficient way?
#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;
}
In R, for the part d = max(abs(temp-x.row(j)));
, the equavilent R code I want is
Asuume there is a vector called tem = temp-x.row(j)
d = max(abs(tem[-c(i,j)]))
Upvotes: 0
Views: 173