I'm having some problems with Position
.
Sometimes it will give an empty list instead of the actual position of the element I am looking for when that element is specified through some other code, but will return the correct position when the element is specified directly as a number as in the minimum working example below.
data = {{0.1, 0.0001683}, {0.2, 0.00035754}, {0.3, 0.00056711}, {0.4,
0.00078986}, {0.5, 0.0010333}, {0.6, 0.0010333}, {0.7,
0.0015758}, {0.8, 0.0018738}, {0.9, 0.0022054}, {1.,
0.0025706}, {1.1, 0.0029788}, {1.2, 0.0034366}, {1.3,
0.0039831}, {1.4, 0.0046433}, {1.5, 0.0055203}, {1.6,
0.0068061}, {1.7, 0.010939}, {1.8, 0.031246}, {1.9, 0.054948}, {2.,
0.076556}, {2.1, 0.098521}, {2.2, 0.12551}, {2.3, 0.1585}, {2.4,
0.1921}, {2.5, 0.22544}, {2.6, 0.25798}, {2.7, 0.28992}, {2.8,
0.32051}, {2.9, 0.35095}, {3., 0.38104}}
interpol = Interpolation[data];
q = FindRoot[interpol[x] == 0.159, {x, 2.9}][[1, 2]]
(*2.3015*)
xlow = Floor[q, 0.1]
(*2.3*)
Position[data[[All, 1]], xlow]
(*{}*)
Position[data[[All, 1]], 2.3]
(*{{23}}*)
When running Mathematica 8 on Windows XP, this code returns {}
for the first output and {{23}}
for the second.
This type of error is mentioned in the Possible Issues section of the documentation for Position
in v8 and v9, but no advice is given.
In[1] := Position[Range[-1, 1, 0.05], 0.1]
Out[1] = {}
I thought this might be a precision or representation issue (i.e., 2.3
vs 23/10
), so I've tried using N
everywhere to get around the issue, but with no success. Does anyone have a nifty work around or solution to this problem?
Position[data[[All, 1]], Nearest[data[[All, 1]], xlow][[1]]]
be acceptable ? $\endgroup$Chop
(see below), it's also worth pointing to this and this. $\endgroup$