3
$\begingroup$

Suppose I have a nested table given by:

t[i_, j_] := j^2 - i^2 - 1;

Table[{j, Table[{i, t[i, j]}, {i, 0, 4, 0.1}]}, {j, 1, 4, 0.1}]

I need to plot j v/s ic, where ic is the first (critical) point at which t[i,j] becomes negative. For example, for j = 1, ic = 0.1, for j = 1.1, ic = 0.5, for j = 1.2, ic = 0.7 and so on.

How can I extract the data from the above-nested table, satisfying the given condition?

$\endgroup$
1
  • 3
    $\begingroup$ Try: dat=Table[{j, Table[{i, t[i, j]}, {i, 0, 4, 0.1}]}, {j, 1, 4, 0.1}]; Function[x, {x[[1]], SelectFirst[x[[2]], #[[2]] < 0 &][[1]]}] /@ dat $\endgroup$ Commented Sep 27, 2022 at 18:24

3 Answers 3

7
$\begingroup$

As of version 10.0, FirstCase is able to be used.

t[i_, j_] := j^2 - i^2 - 1
data = Table[{j, Table[{i, t[i, j]}, {i, 0, 4, 0.1}]}, {j, 1, 4, 0.1}];

{#, FirstCase[#2, {i_, _?Negative} :> i]} & @@@ data

{{1., 0.1}, {1.1, 0.5}, {1.2, 0.7}, {1.3, 0.9}, {1.4, 1.}, {1.5, 1.2}, {1.6, 1.3}, {1.7, 1.4}, {1.8, 1.5}, {1.9, 1.7}, {2., 1.8}, {2.1, 1.9}, {2.2, 2.}, {2.3, 2.1}, {2.4, 2.2}, {2.5, 2.3}, {2.6, 2.4}, {2.7, 2.6}, {2.8, 2.7}, {2.9, 2.8}, {3., 2.9}, {3.1, 3.}, {3.2, 3.1}, {3.3, 3.2}, {3.4, 3.3}, {3.5, 3.4}, {3.6, 3.5}, {3.7, 3.6}, {3.8, 3.7}, {3.9, 3.8}, {4., 3.9}}

$\endgroup$
5
$\begingroup$

Try this:

t[i_, j_] := j^2 - i^2 - 1
data = Table[{j, Table[{i, t[i, j]}, {i, 0, 4, 0.1}]}, {j, 1, 4, 0.1}];
Map[Composition[#[[1 ;; 2]] &, Flatten], Table[{data[[k, 1]], First[Map[If[#[[2]] < 0, #, Nothing] &, Map[#[[-1]] &, data][[k]]]]}, {k, 1, Length[data]}]]
(*{{1., 0.1}, {1.1, 0.5}, {1.2, 0.7}, {1.3, 0.9}, {1.4, 1.}, {1.5, 1.2}, {1.6, 1.3}, {1.7, 1.4}, {1.8, 1.5}, {1.9, 1.7}, {2., 1.8}, {2.1, 1.9}, {2.2, 2.}, {2.3, 2.1}, {2.4, 2.2}, {2.5, 2.3}, {2.6, 2.4}, {2.7, 2.6}, {2.8, 2.7}, {2.9, 2.8}, {3., 2.9}, {3.1, 3.}, {3.2, 3.1}, {3.3, 3.2}, {3.4, 3.3}, {3.5, 3.4}, {3.6, 3.5}, {3.7, 3.6}, {3.8, 3.7}, {3.9, 3.8}, {4., 3.9}}*)
$\endgroup$
3
$\begingroup$
t[i_, j_] := j^2 - i^2 - 1;


data = Table[{j, Table[{i, t[i, j]}, {i, 0, 4, 0.1}]}, {j, 1, 4, 0.1}];

First positions of negative elements

p = Last @ FirstPosition[#, {_, _?Negative}] & /@ data

{2, 6, 8, 10, 11, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}

Table[{data[[i, 1]], data[[i, 2, p[[i]], 1]]}, {i, Length[data]}]

{{1., 0.1}, {1.1, 0.5}, {1.2, 0.7}, {1.3, 0.9}, {1.4, 1.}, {1.5, 1.2}, {1.6, 1.3}, {1.7, 1.4}, {1.8, 1.5}, {1.9, 1.7}, {2., 1.8}, {2.1, 1.9}, {2.2, 2.}, {2.3, 2.1}, {2.4, 2.2}, {2.5, 2.3}, {2.6, 2.4}, {2.7, 2.6}, {2.8, 2.7}, {2.9, 2.8}, {3., 2.9}, {3.1, 3.}, {3.2, 3.1}, {3.3, 3.2}, {3.4, 3.3}, {3.5, 3.4}, {3.6, 3.5}, {3.7, 3.6}, {3.8, 3.7}, {3.9, 3.8}, {4., 3.9}}

$\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.