You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this section, the code to calculate "Euclidean distance between a value and a vector of values" is provided as follows:
pdistR <- function(x, ys) {
sqrt((x - ys) ^ 2)
}
While the code is not incorrect, it is quite inefficient. The square-root and square operations almost effectively cancel out. The euclidean distance between two values x and y in one-dimensional space is simply abs(x - y) and is more efficiently calculated as such. One should probably optimize expressions algebraically before trying to optimize this code with Rcpp.
Given that Euclidean distance is more often used and discussed in spaces with higher than one dimension, a more useful function would probably take a vector input and a matrix input and return a vector output. Perhaps a different example for a vector-input and vector-output function should be used, such as mean-centering a vector:
centerR <- function(ys) {
ys - mean(ys)
}
The text was updated successfully, but these errors were encountered:
I agree with your points, but I don't think centerR is an adequate replacement because this example needs a function with a vector argument and a scalar argument.
In this section, the code to calculate "Euclidean distance between a value and a vector of values" is provided as follows:
While the code is not incorrect, it is quite inefficient. The square-root and square operations almost effectively cancel out. The euclidean distance between two values x and y in one-dimensional space is simply abs(x - y) and is more efficiently calculated as such. One should probably optimize expressions algebraically before trying to optimize this code with Rcpp.
Given that Euclidean distance is more often used and discussed in spaces with higher than one dimension, a more useful function would probably take a vector input and a matrix input and return a vector output. Perhaps a different example for a vector-input and vector-output function should be used, such as mean-centering a vector:
The text was updated successfully, but these errors were encountered: