Skip to content
Back to formatted view

Raw Message

Message-ID: <4B533438.1050307@gmx.de>
Date: 2010-01-17T16:00:56Z
From: Andreas Wittmann
Subject: enty-wise closest element

Dear R-users,

i have a simple problem maybe, but i don't see the solution. i want to 
find the entry-wise closest element of an vector compared with another.

ind1<-c(1,4,10)
ind2<-c(3,5,11)

for (i in length(ind2):1)
{
  print(which.min(abs(ind1-ind2[i])))
}

for ind2[3] it should be ind1[3] 10, for ind2[2] it should be ind1[2] 4 
and for ind2[1] it should be ind1[1] 1. but with the for-loop above i 
get ind1[3], ind1[2] and ind1[2].

any suggestions are quite welcome.

best regards

Andreas