You can achieve the same end with DeleteCases
or Cases
.
f
signals a coordinate that is out of bounds.
g
signals that a coordinate is in bounds.
f[n_] := n < 1 \[Or] n > 9
g[n_] := 1 <= n <= 9
Four ways to filter out the outlying points:
v[k]
generates 10^k values between {0, 0} and {10, 10})
v[k_] := Table[{r, r}, {10^k}];
Delete the points that have an out-of-bounds coordinate.
DeleteCases[v[k], {x_, y_} /; f[x] \[Or] f[y]]
Delete the points that are not in-bounds.
DeleteCases[v[k], {x_, y_} /; \[Not] (g[x] \[And] g[y])]
Keep points that are not out-of-bounds.
Cases[v[k], {x_, y_} /; \[Not] (f[x] \[Or] f[y])]
Keep points that are in-bounds.
Cases[v[k], {x_, y_} /; (g[x] \[And] g[y])]
Timings
r = Table[{AbsoluteTiming[DeleteCases[v[k], {x_, y_} /; f[x] \[Or] f[y]];],
AbsoluteTiming[DeleteCases[v[k], {x_, y_} /; \[Not] (g[x] \[And]g[y])];],
AbsoluteTiming[Cases[v[k], {x_, y_} /; \[Not] (f[x] \[Or] f[y])];],
AbsoluteTiming[Cases[v[k], {x_, y_} /; (g[x] \[And] g[y])];]}, {k, 3, 7}]
Grid@Prepend[ r[[All, All, 1]], {"Delete f", "Delete not-g", "Cases not-f", "Cases g"}]
The results with DeleteCases and Cases (from 10^3 to 10^7 points)
show that DeleteCases
with function g
gives the fastest analysis.
