Select 从mathematica中的表中选择数据

Select 从mathematica中的表中选择数据,select,wolfram-mathematica,Select,Wolfram Mathematica,我试图编写一个函数,它将选择表中满足条件的第一个元素。例如,如果给我下表,第一列是时间,第二列是感染疾病的人数,我想写一个参数,返回至少100人感染的时间 0 1 1 2 2 4 3 8 4 15 5 29 6 50 7 88 8 130 9 157 10 180 11 191 12 196 13 199 14 200 所以从这张表中,我想让arguemnt告诉我,在8秒钟内,至少有100人被感染。我尝试使用SELECT来执行此操作,但我不确定

我试图编写一个函数,它将选择表中满足条件的第一个元素。例如,如果给我下表,第一列是时间,第二列是感染疾病的人数,我想写一个参数,返回至少100人感染的时间

0   1
1   2
2   4
3   8
4   15
5   29
6   50
7   88
8   130
9   157
10  180
11  191
12  196
13  199
14  200

所以从这张表中,我想让arguemnt告诉我,在8秒钟内,至少有100人被感染。我尝试使用SELECT来执行此操作,但我不确定如何将SELECT与包含2列的表一起使用,并让它根据第二列中的条件在第一列中返回值

这里有几种不同的方法,假设我正确地解释了您的数据

In[3]:= data = {{0,1},{1,2},{2,4},{3,8},{4,15},{5,29},{6,50},{7,88},{8,130},{9,157},{10,180},{11,191},{12,196},{13,199},{14,200}};

In[8]:= Cases[data, {_, _?(#>=100&)}, 1, 1][[1, 1]]
Out[8]= 8

In[9]:= Select[data, #[[2]]>=100&, 1][[1, 1]]
Out[9]= 8

我建议你仔细阅读第[]部分,以便更好地理解这一点

使用替换规则的替代方法是

ImportString["0 1 1 2 2 4 3 8 4 15 5 29 6 50 7 88 8 130 9 157 10 180 11 191 12 196 13 199 14 200", "Table"];
Partition[Flatten[%], 2]
% /. {___, x : {_, _?(# >= 100 &)}, ___} :> x
Mathematica搜索模式的算法确保返回第一个这样的情况。如果需要所有案例,则可以使用ReplaceList。 我建议您阅读和上的教程


编辑:ImportString也适用于新格式化的数据,但您不再需要使用分区。

您也可以使用简单的嵌套

data = {{0,1},{1,2},{2,4},{3,8},{4,15},{5,29},{6,50},{7,88},{8,130},{9,157},{10,180},
 {11,191},{12,196},{13,199},{14,200}};
NestWhile[# + 1 &, 1, data[[#, 2]] < 100 &] - 1

我相信有一种比已经给出的更快的方法,但首先,约书亚的案例方法可以通过使用/;而不是&为了测试

这就是我建议的解决方案编辑:为清晰起见添加空白,因为双括号在这里不设置格式:

dat[[
  Position[
    dat[[All, 2]],
    x_ /; x >= 100,
    1, 1
  ][[1, 1]],
  1
]]
以下是提供的各种方法的时间安排。请注意,/。方法只运行一次,而其他方法则运行两次。因此,在第一次测试中,它比位置法慢100倍。另外,nestwile方法只返回索引,而不是实际的第一列元素

In[]:= 
dat = {Range[5000], Sort@RandomInteger[1*^6, 5000]} // Transpose;
lim = 300000; loops = 100;
dat /. {___, {x_, _?(# >= lim &)}, ___} :> x; // Timing
Do[  Cases[dat, {_, _?(# >= lim &)}, 1, 1][[1, 1]]  , {loops}] // Timing
Do[  Cases[dat, {_, y_ /; y >= lim}, 1, 1][[1, 1]]  , {loops}] // Timing
Do[  Select[dat, #[[2]] >= lim &, 1][[1, 1]]  , {loops}] // Timing
Do[  NestWhile[# + 1 &, 1, dat[[#, 2]] < lim &]  , {loops}] // Timing
Do[  dat[[Position[dat[[All, 2]], x_ /; x >= lim, 1, 1][[1, 1]], 1]]  , {loops}] // Timing

Out[]= {0.125, Null}

Out[]= {0.438, Null}

Out[]= {0.406, Null}

Out[]= {0.469, Null}

Out[]= {0.281, Null}

Out[]= {0.125, Null}

这真的是一个数学问题吗?+1,模式的巧妙运用。因为,她只想要第一列中的值,所以我将您的模式更改为{{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。否则,没有更改。+1,因为替换规则通常比Cases、Select或Position快得多。@Timo模式匹配实际上比Joshua给出的Cases和Select慢得多。
In[]:= 
dat = {Range[35000], Sort@RandomInteger[1*^6, 35000]} // Transpose;
lim = 300000; loops = 25;
Do[  Cases[dat, {_, _?(# >= lim &)}, 1, 1][[1, 1]]  , {loops}] // Timing
Do[  Cases[dat, {_, y_ /; y >= lim}, 1, 1][[1, 1]]  , {loops}] // Timing
Do[  Select[dat, #[[2]] >= lim &, 1][[1, 1]]  , {loops}] // Timing
Do[  NestWhile[# + 1 &, 1, dat[[#, 2]] < lim &]  , {loops}] // Timing
Do[  dat[[Position[dat[[All, 2]], x_ /; x >= lim, 1, 1][[1, 1]], 1]]  , {loops}] // Timing

Out[]= {0.734, Null}

Out[]= {0.641, Null}

Out[]= {0.734, Null}

Out[]= {0.5, Null}

Out[]= {0.266, Null}
In[]:= SameQ[
         Select[dat, #[[2]] >= lim &, 1][[1, 1]],
         dat[[Position[dat[[All, 2]], x_ /; x >= lim, 1, 1][[1, 1]], 1]]
       ]

Out[]= True