0

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)]))
Nicolas H
  • 535
  • 3
  • 13
  • 1
    The example does not work for me, i.e. there is no `outrows`. Could you provide a minimum working example that also includes the invocation of `Mydist` in `R` and the result you expect. – Jon Nagra Oct 26 '20 at 12:04
  • @JonNagra Hi, thank you for your comment, I just edit the question – Nicolas H Oct 26 '20 at 13:08
  • 1
    See https://stackoverflow.com/questions/34550395/rcpp-rcpparmadillo-removing-non-contiguous-elements-from-a-vector-based-on-posi – thc Oct 27 '20 at 22:22
  • @NicolasH with a minimum working example with the invocation of `Mydist`, I meant a piece of code that would compile the `Rcpp` function and call it from `R` (even if the i & j aren't removed) – Jon Nagra Oct 28 '20 at 11:01

0 Answers0