0
$\begingroup$

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?

$\endgroup$
4
  • $\begingroup$ @rainer, In your example the first element of ti is not infinity. What is $t_0:=\infty$ supposed to be? $\endgroup$
    – halirutan
    Commented Feb 10, 2013 at 11:56
  • $\begingroup$ 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. $\endgroup$
    – rainer
    Commented Feb 10, 2013 at 12:07
  • $\begingroup$ rainer, with your new indexing, "I know that j=5 ..." should change to "I know that j=6 ..."? $\endgroup$
    – kglr
    Commented Feb 10, 2013 at 12:29
  • $\begingroup$ 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. $\endgroup$
    – rainer
    Commented Feb 10, 2013 at 16:24

3 Answers 3

3
$\begingroup$
 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} *)
$\endgroup$
1
  • $\begingroup$ Thanks, I figured out how to do it with your first solution! $\endgroup$
    – rainer
    Commented Feb 10, 2013 at 17:02
3
$\begingroup$

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}}
$\endgroup$
2
$\begingroup$

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]];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.