return index from a vector of the value closest to a given element

I would use which.min

which.min(abs(x-0.4))

This will return the first index of the closest number to 0.4.

Leave a Comment