Skip to content

distance between two matrices

2 messages · "Hüsing, Johannes", Brian Ripley

#
make the lines below a function of a vector argument and 
apply it over the rows of B.

?apply for more info. You'll want to know about apply if
you want to avoid loops (which is a good approach).
Thing is, the above approach requires all data to be in main memory.
i hope this is not a problem.
#
On Wed, 28 Jan 2004, "H?sing, Johannes" wrote:

            
Unfortunately apply() is a wrapper for a for() loop, so will not help much 
(if at all).
You can improve this a bit: see predict.qda.
A 140K x 2 array takes up 1.6Mb, and R needs 10x that to run at all.

Several people have mentioned knn1 as a C-level equivalent of the loops
(and I timed it as probably fast enough).  Roger Bivand mentioned
quadtrees, and that is one of a class of possible solutions if you need
extra speed.  Which member of that class is suitable depends on the
spatial distribution of A and B (viewing the rows as 2D points), but it is
hard to do very much better for only around a 1000 reference points.