distance between two matrices
On Wed, 28 Jan 2004, "H?sing, Johannes" wrote:
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] On Wed, 28 Jan 2004, "H?sing, Johannes" wrote:
?apply for more info. You'll want to know about apply if you want to avoid loops (which is a good approach).
Unfortunately apply() is a wrapper for a for() loop, so will not help much (if at all).
whoops; so is the choice between both a matter of style? Or is it implementation-specific for R, and not generally true for S?
It is not generally true for S. The case studies in chapter 7 of `S Programming' show that efficient ways are implementation-dependent. In some versions of S-PLUS (e.g. 3.4) apply was much faster and in some (5.0) it was much slower. Using lapply() (which has a C-level loop) is sometimes rather faster in R. In this particular example garbage collection is taking about 50% of time of the pure R solution, so exactly what is done in what order can matter. knn1 is 20-30x faster since it works in place with space allocated just once.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595