Rafael Castro
Rafael Castro

Reputation: 609

Find the closest two numbers and its the distance

I have a file like:

0.000000
0.000051
0.000076
0.000102
0.000152
0.000178
0.000203
0.000229
0.000254
0.000279
0.000305
0.000356
0.000381
0.000406

And I wanna find the two numbers and its closest distance. I'm using shell script. At the time I have:

points.dat cat | cut-f1 | sort | uniq

In next case I would like to consider two columns. So would a column for the x-axis and one for the y axis. It's like the Closest pair of points problem. I would like help in that too.

Thank you for helping!

Upvotes: 0

Views: 90

Answers (2)

FloHimself
FloHimself

Reputation: 291

According to your current input file this will calculate the closest distance:

sort -u points.dat \
| xargs -n2 -J{} echo {} "- _1 * p" \
| dc \
| sort -u \
| sed '1!d;s/^\./0./'

You should post your final input file to get a more accurate answer.

Edit: Well, this would be a more accurate distance:

sort -n -u points.dat \
| sed '1!p' \
| paste - - \
| head -n-1 \
| xargs -n2 -I{} echo {} "- _1 * p" \
| dc \
| sort -n -u \
| sed '1!d;s/^\./0./'

Upvotes: 1

Kei Minagawa
Kei Minagawa

Reputation: 4521

This is comment not answer.

Use this output when you check your algorithm.

Python code:

import itertools

data = [
0.000000, #data[0]
0.000051, #data[1]
0.000076, #data[2]
0.000102, #data[3]
0.000152, #data[4]
0.000178, #data[5]
0.000203, #data[6]
0.000229, #data[7]
0.000254, #data[8]
0.000279, #data[9]
0.000305, #data[10]
0.000356, #data[11]
0.000381, #data[12]
0.000406  #data[13]
]

for j in sorted((abs(i[0][0]-i[1][0]), (i[0][1],i[1][1])) for i in itertools.combinations( zip(data,range(len(data))), 2)):
    print str(j[1]).ljust(8), '->',j[0]#indices -> distance

output:

(7, 8)   -> 2.5e-05
(1, 2)   -> 2.5e-05
(5, 6)   -> 2.5e-05
(8, 9)   -> 2.5e-05
(11, 12) -> 2.5e-05
(12, 13) -> 2.5e-05
(4, 5)   -> 2.6e-05
(9, 10)  -> 2.6e-05
(2, 3)   -> 2.6e-05
(6, 7)   -> 2.6e-05
(7, 9)   -> 5e-05
(3, 4)   -> 5e-05
(11, 13) -> 5e-05
(4, 6)   -> 5.1e-05
(6, 8)   -> 5.1e-05
(8, 10)  -> 5.1e-05
(10, 11) -> 5.1e-05
(0, 1)   -> 5.1e-05
(1, 3)   -> 5.1e-05
(5, 7)   -> 5.1e-05
(7, 10)  -> 7.6e-05
(3, 5)   -> 7.6e-05
(0, 2)   -> 7.6e-05
(2, 4)   -> 7.6e-05
(5, 8)   -> 7.6e-05
(6, 9)   -> 7.6e-05
(10, 12) -> 7.6e-05
(9, 11)  -> 7.7e-05
(4, 7)   -> 7.7e-05
(3, 6)   -> 0.000101
(1, 4)   -> 0.000101
(5, 9)   -> 0.000101
(10, 13) -> 0.000101
(2, 5)   -> 0.000102
(4, 8)   -> 0.000102
(6, 10)  -> 0.000102
(8, 11)  -> 0.000102
(9, 12)  -> 0.000102
(0, 3)   -> 0.000102
(7, 11)  -> 0.000127
(1, 5)   -> 0.000127
(2, 6)   -> 0.000127
(3, 7)   -> 0.000127
(4, 9)   -> 0.000127
(5, 10)  -> 0.000127
(8, 12)  -> 0.000127
(9, 13)  -> 0.000127
(7, 12)  -> 0.000152
(0, 4)   -> 0.000152
(1, 6)   -> 0.000152
(3, 8)   -> 0.000152
(8, 13)  -> 0.000152
(4, 10)  -> 0.000153
(6, 11)  -> 0.000153
(2, 7)   -> 0.000153
(7, 13)  -> 0.000177
(3, 9)   -> 0.000177
(0, 5)   -> 0.000178
(2, 8)   -> 0.000178
(5, 11)  -> 0.000178
(6, 12)  -> 0.000178
(1, 7)   -> 0.000178
(0, 6)   -> 0.000203
(1, 8)   -> 0.000203
(2, 9)   -> 0.000203
(3, 10)  -> 0.000203
(5, 12)  -> 0.000203
(6, 13)  -> 0.000203
(4, 11)  -> 0.000204
(1, 9)   -> 0.000228
(5, 13)  -> 0.000228
(2, 10)  -> 0.000229
(4, 12)  -> 0.000229
(0, 7)   -> 0.000229
(0, 8)   -> 0.000254
(1, 10)  -> 0.000254
(3, 11)  -> 0.000254
(4, 13)  -> 0.000254
(0, 9)   -> 0.000279
(3, 12)  -> 0.000279
(2, 11)  -> 0.00028
(3, 13)  -> 0.000304
(0, 10)  -> 0.000305
(1, 11)  -> 0.000305
(2, 12)  -> 0.000305
(1, 12)  -> 0.00033
(2, 13)  -> 0.00033
(1, 13)  -> 0.000355
(0, 11)  -> 0.000356
(0, 12)  -> 0.000381
(0, 13)  -> 0.000406

So the smallest set is (data[7], data[8]).

And from the output if you sort the data before calc, you can easily find the minimum distance like this.

#This is pseudocode.
x[n] = data[n] - data[n-1]
min(x)

Upvotes: 2

Related Questions