Calculating Minimum Absolute Difference of Two Numeric Vectors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculating Minimum Absolute Difference of Two Numeric Vectors

Dario Strbenac-2
Good day,

What is a fast and efficient way to calculate the minimum absolute difference between two vectors of numbers? The two vectors have unequal length. I would also like to know the index of the first vector and the second vector which results in the minimum absolute difference. For example:

x <- rpois(500, 100)
y <- rpois(300, 30)

Is there a much faster way than a nested for loop without resorting to Rcpp?

--------------------------------------
Dario Strbenac
University of Sydney
Camperdown NSW 2050
Australia

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating Minimum Absolute Difference of Two Numeric Vectors

Jeff Newmiller
This looks more like a code challenge than a real problem, but anyway Rcpp seems unnecessary.

x <- (2:5)^2/3
y <- (1:6)+0.1
ad <- function( a, b ) {
  abs( a - b )
}
M <- outer( x, y, FUN=ad )
which( M==min(M), arr.ind = TRUE )

On August 18, 2020 12:00:09 AM PDT, Dario Strbenac <[hidden email]> wrote:

>Good day,
>
>What is a fast and efficient way to calculate the minimum absolute
>difference between two vectors of numbers? The two vectors have unequal
>length. I would also like to know the index of the first vector and the
>second vector which results in the minimum absolute difference. For
>example:
>
>x <- rpois(500, 100)
>y <- rpois(300, 30)
>
>Is there a much faster way than a nested for loop without resorting to
>Rcpp?
>
>--------------------------------------
>Dario Strbenac
>University of Sydney
>Camperdown NSW 2050
>Australia
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating Minimum Absolute Difference of Two Numeric Vectors

PIKAL Petr
In reply to this post by Dario Strbenac-2
Hi

maybe

min(abs(outer(x,y, "-")))

If you want indices

mm <- min(abs(outer(x,y, "-")))
which(abs(outer(x,y, "-"))== mm, arr.ind=TRUE)

And in original vectors

x[which(abs(outer(x,y, "-"))== mm, arr.ind=TRUE)[1]]
y[which(abs(outer(x,y, "-"))== mm, arr.ind=TRUE)[2]]

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Dario Strbenac
> Sent: Tuesday, August 18, 2020 9:00 AM
> To: [hidden email]
> Subject: [R] Calculating Minimum Absolute Difference of Two Numeric
Vectors
>
> Good day,
>
> What is a fast and efficient way to calculate the minimum absolute
difference
> between two vectors of numbers? The two vectors have unequal length. I
> would also like to know the index of the first vector and the second
vector
> which results in the minimum absolute difference. For example:
>
> x <- rpois(500, 100)
> y <- rpois(300, 30)
>
> Is there a much faster way than a nested for loop without resorting to
Rcpp?

>
> --------------------------------------
> Dario Strbenac
> University of Sydney
> Camperdown NSW 2050
> Australia
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.