Mar*_*ary 3 wolfram-mathematica
例如,我在mathematica中有一对值列表List= {{3,1},{5,4}}.
如果第二个元素未达到阈值,如何更改第一个元素(3和5).例如,如果第二部分低于2,那么我希望第一部分变为零.所以列表然后= {{0,1},{5,4}}.其中一些列表非常长,所以不幸的是,手动执行它不是一个选项.
从概念上讲,一般的方法是使用Map.在你的情况下,代码将是
In[13]:= lst = {{3, 1}, {5, 4}}
Out[13]= {{3, 1}, {5, 4}}
In[14]:= thr = 2
Out[14]= 2
In[15]:= Map[{If[#[[2]] < thr, 0, #[[1]]], #[[2]]} &, lst]
Out[15]= {{0, 1}, {5, 4}}
Run Code Online (Sandbox Code Playgroud)
#这里的符号代表函数参数.你可以阅读更多关于纯函数这里.双方括号代表零件提取.您可以通过在级别1上使用Apply来使其更简洁 ,其缩写为@@@:
In[27]:= {If[#2 < thr, 0, #], #2} & @@@ lst
Out[27]= {{0, 1}, {5, 4}}
Run Code Online (Sandbox Code Playgroud)
但请注意,对于大型数字列表,第一种方法要快几倍.这是一个更快,但更隐蔽的方法:
In[29]:= Transpose[{#[[All, 1]]*UnitStep[#[[All, 2]] - thr], #[[All, 2]]}] &[lst]
Out[29]= {{0, 1}, {5, 4}}
Run Code Online (Sandbox Code Playgroud)
它更快,因为它使用非常优化的矢量化操作,一次应用于所有子列表.最后,如果你想要最终性能,这个编译为C版本的程序将是另一个更快的因素:
fn = Compile[{{lst, _Integer, 2}, {threshold, _Real}},
Module[{copy = lst, i = 1},
For[i = 1, i <= Length[lst], i++,
If[copy[[i, 2]] < threshold, copy[[i, 1]] = 0]];
copy], CompilationTarget -> "C", RuntimeOptions -> "Speed"]
Run Code Online (Sandbox Code Playgroud)
你用它作为
In[32]:= fn[lst, 2]
Out[32]= {{0, 1}, {5, 4}}
Run Code Online (Sandbox Code Playgroud)
对于最后一个,您需要在计算机上安装C编译器.
| 归档时间: |
|
| 查看次数: |
1799 次 |
| 最近记录: |