# Finding an index with certain conditions

EDIT: I need to find out an index i which depends on certain conditions and I'm not able to implement it. I have a list (time points) $t_i$ and two constants $a,c$ the length of the list $t_i$ is $n$.

So now I want to find the index $1 \leq i < n$, which can be found according to $t_{n-i}+a<c<t_{n-(i+1)}$ where $t_0:=\infty$.

For example I know that i=5in this example, but I would like to have a function which tells me that:

c = 0.07875
a = 0.01125
ti = {0.056249999999999994, 0.045, 0.03375, 0.0225, 0.01125, 0.}
n=6


Do you have any tips how to implement it?

-
@rainer, In your example the first element of ti is not infinity. What is $t_0:=\infty$ supposed to be? – halirutan Feb 10 '13 at 11:56
It's difficult for me to explain it properly but i try my best: if n-(i+1)=0 we have t_0which does not exist so we set it to Infinity. Basically I did a coordination transformation and need to find the index (time) in the new system between which tis my c is. – rainer Feb 10 '13 at 12:07
rainer, with your new indexing, "I know that j=5 ..." should change to "I know that j=6 ..."? – kglr Feb 10 '13 at 12:29
mho, no actually it should be i=5, because if i=6 i would get t_(n-i-1)=t_(-1)which would make no sense,ishould be lower than n. – rainer Feb 10 '13 at 16:24

 Pick[#, ti[[n - (# - 1)]] + a < c < If[# == n, Infinity, ti[[n - (#)]]] & /@ #]&[Range[Length[ti]]]


or

 Select[Range[Length[ti]], ti[[n - (# - 1)]] + a < c < If[# == n, Infinity, ti[[n - (#)]]] &]
(* {6} *)

-
Thanks, I figured out how to do it with your first solution! – rainer Feb 10 '13 at 17:02

I'm really not sure I understand your question but maybe by pure luck I'll hit upon something that helps you.

c = 0.07875
a = 0.01125
ti = {0.056249999999999994, 0.045, 0.03375, 0.0225, 0.01125, 0.};

AppendTo[ti, ∞]

Position[
# + a < c < #2 & @@@ Partition[ti, 2, 1],
True
]

{{6}}

-

Can create a large surrogate for infinity and use a zero-order interpolation.

inf = 10.^10;

func = Interpolation[
Transpose[{Prepend[data, inf], Range[Length[data], 0, -1]}]
, InterpolationOrder -> 0];


Exampless:

c = 0.07875;
a = 0.01125;

func[.04]

(* Out[122]= 4. *)

func[a + c]

(* Out[123]= 6. *)


For large data sets this method will be generally faster than linear search approaches. For example, try it on

data2 = Accumulate[RandomReal[1, 10^6]];

-