给定数据矩阵x,我想计算x的任意两个行之间的成对距离矩阵。我有以下代码,这些代码来自此处稍微调整代码。
#include <Rcpp.h>
#include <cmath>
#include <algorithm>
using namespace Rcpp;
// generic function for l1_distance
template <typename InputIterator1, typename InputIterator2>
inline double l1_distance(InputIterator1 begin1, InputIterator1 end1,
InputIterator2 begin2) {
double rval = 0;
InputIterator1 it1 = begin1;
InputIterator2 it2 = begin2;
while (it1 != end1) {
double d1 = *it1++;
double d2 = *it2++;
rval += abs(d1 - d2);
}
return rval;
}
// [[Rcpp::export]]
NumericMatrix rcpp_l1_distance(NumericMatrix mat) {
// allocate the matrix we will return
NumericMatrix rmat(mat.nrow(), mat.nrow());
for (int i = 0; i < rmat.nrow(); i++) {
for (int j = 0; j < i; j++) {
NumericMatrix::Row row1 = mat.row(i);
NumericMatrix::Row row2 = mat.row(j);
double d = l1_distance(row1.begin(), row1.end(), row2.begin());
rmat(i,j) = d;
rmat(j,i) = d;
}
}
return rmat;
}
问题在于此代码返回所有整数值的矩阵。整数值似乎与我想要的距离值呈正相关,这使其更令人困惑。我还计算了一个成对的L2距离矩阵和成对归一化的L1距离(将两个行之间的L1距离除以L1-norm的总和(矩阵,它们的表现都如预期的。
有人可以告诉我我犯了哪一部分?
您可以执行以下操作以获得奇怪的结果
library(Rcpp)
sourceCpp("distance.cpp") #the file containing the cpp code above
X = matrix(rnorm(16), 4, 4)
rcpp_l1_distance(X)
预先感谢!
编译您的代码给我以下警告:
> Rcpp::sourceCpp('Desktop/mat.cpp')
mat.cpp:16:15: warning: using integer absolute value function 'abs' when argument is of floating point type [-Wabsolute-value]
rval += abs(d1 - d2);
^
mat.cpp:16:15: note: use function 'std::abs' instead
rval += abs(d1 - d2);
^~~
std::abs
mat.cpp:16:15: warning: using integer absolute value function 'abs' when argument is of floating point type [-Wabsolute-value]
rval += abs(d1 - d2);
^
mat.cpp:30:18: note: in instantiation of function template specialization 'l1_distance<Rcpp::MatrixRow<14>::iterator, Rcpp::MatrixRow<14>::iterator>' requested here
double d = l1_distance(row1.begin(), row1.end(), row2.begin());
^
mat.cpp:16:15: note: use function 'std::abs' instead
rval += abs(d1 - d2);
^~~
std::abs
2 warnings generated.
...暗示abs
适用于整数,请参阅此帮助页,您可以使用Fabs,或者可以使用STD :: ABS,或者您可以使用Sugar的operator-
,abs
和sum
:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix rcpp_l1_distance(NumericMatrix mat) {
// allocate the matrix we will return
NumericMatrix rmat(mat.nrow(), mat.nrow());
for (int i = 0; i < rmat.nrow(); i++) {
NumericMatrix::Row row1 = mat.row(i);
for (int j = 0; j < i; j++) {
rmat(j,i) = rmat(i,j) = sum( abs(row1 - mat.row(j) )) ;
}
}
return rmat;
}