Here is one approach if I understand your requirements correctly.
ind1<-c(1,4,10)
ind2<-c(3,5,11)
m <- expand.grid(ind2=ind2,ind1=ind1)
m$diff <- (m$ind2 - m$ind1)
f <- function(x) {
min_idx <- which.min(x$diff[x$diff > 0 & x$diff < x$ind2])
list(c(elem=unique(x$ind2),min.value=x$i
On Sun, 17 Jan 2010, Andreas Wittmann wrote:
Thank you very much for your quick answers.
Sorry, but i forgot to explain i want to find the closest and smaller element
for each entry, so ind1[3] is smaller as 11 and close to 11, ind1[2] is
smaller as 5 and close to 5 and ind1[1] is smaller tha
On Jan 17, 2010, at 12:30 PM, Andreas Wittmann wrote:
Thank you very much for your quick answers.
Sorry, but i forgot to explain i want to find the closest and
smaller element for each entry, so ind1[3] is smaller as 11 and
close to 11, ind1[2] is smaller as 5 and close to 5 and ind1[1] is
Thank you very much for your quick answers.
Sorry, but i forgot to explain i want to find the closest and smaller
element for each entry, so ind1[3] is smaller as 11 and close to 11,
ind1[2] is smaller as 5 and close to 5 and ind1[1] is smaller than 3 and
close to 3.
best regards
Andreas
Is it not giving you the location of the minimum value not the value?
See
ind1<-c(1,4,10)
ind2<-c(3,5,11)
i=3
(ind1-ind2[i])
(aa <- abs(ind1-ind2[i]))
which.min(aa)
--- On Sun, 1/17/10, Andreas Wittmann wrote:
> From: Andreas Wittmann
> Subject: [R] enty-wise closest element
On Jan 17, 2010, at 11:00 AM, Andreas Wittmann wrote:
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(
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,
7 matches
Mail list logo