“选择”比较 Mathematica 中的相邻元素
在 Mathematica 中,“选择”命令仅允许为列表中的每个元素单独定义选择标准。
我想指定一个标准,该标准分别依赖于前一个和/或下一个元素的函数以及所有元素的函数。第一个和最后一个元素无法以这种方式进行测试,但无论如何都应该选择它们。
迭代地这样做可能不会有问题,我想先尝试一下它的功能。
我会像这样使用它:
Select[list,FirstQ||LastQ,Func1[#-1,#]&&Func2[#,#1]&&Func3[list]&]
In Mathematica the command Select only lets define a selection criterion for each element of a list on its own.
I want to specify a criterion that is dependend of a function of the previous and/or next element and a function on all elements, respectively. The first and last element cannot be tested that way, but they should be selected anyway.
Doing that iteratively probably would not be a problem, I want to try it functional first.
I would imaging using it somehow like that:
Select[list,FirstQ||LastQ,Func1[#-1,#]&&Func2[#,#1]&&Func3[list]&]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用分区功能。要使列表中的每个元素与其直接邻居分组,您可以这样做
来得到:
知道这一点,我们可以创建一个与您的规范大致匹配的“与邻居一起选择”函数:
请注意,在 trinaryfunc 的参数中,#2 是列表元素本身,#1 是左邻居,#3 是右邻居。
最好将其概括为使用任意数量的邻居,而不仅仅是直接邻居,但是您需要一种比 {#1, #2, #3} 的模拟更好的方式来引用它们。
I suggest using the Partition function. To get each element of a list grouped with its immediate neighbors you can do this
to get this:
Knowing that, we can make a "select with neighbors" function that roughly matches your spec:
Note that in the arguments to trinaryfunc, #2 is the list element itself, #1 is the left neighbor, and #3 is the right neighbor.
It would be nice to generalize this to use any number of neighbors, not just the immediate neighbors, but then you'd want a better way to refer to them than the analog of {#1, #2, #3}.