I need some help with simplification of a function.
Nearest can be very handy, but does not have level specification. My problem is simple. I have the following data consisting of integers and binary vectors:
dat = Table[{RandomInteger[100], RandomInteger[1, 15]}, {10}];
dat // Column
Out[] =
{{21, {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1}},
{15, {1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0}},
{55, {0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0}},
{79, {1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1}},
{55, {1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1}}}
I also have a test vector:
tst = RandomInteger[1, 15]
Out[] = {1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0}
I need to find a vector in my data nearest to test and return it and its corresponding integer. If several solutions exist I take the first one. I came up with this
f[dat_, tst_] := First[Extract[dat, Position[#, First[Nearest[#, tst]]] &[dat[[All, 2]]]]]
f[dat, tst]
Out[] = {15, {1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0}}
Which works but feels rather convoluted. Can anyone suggest an alternative solution?