Elementwise[f][{x1,…,xn},{y1,…,yn}]
给出 {f[x1,y1],…,f[xn,yn]}.
Elementwise[f][arg1,…]
逐项作用于 arg1,… 中的所有列表.
Elementwise
Elementwise[f][{x1,…,xn},{y1,…,yn}]
给出 {f[x1,y1],…,f[xn,yn]}.
Elementwise[f][arg1,…]
逐项作用于 arg1,… 中的所有列表.
更多信息
- Elementwise 通常用于使不可列函数的行为变得可列.
范例
打开所有单元 关闭所有单元基本范例 (4)
Elementwise[Max][{1, -1, 3, -4, 5}, 0]Elementwise[Less][{1, 0, 2, -1}, 1]Elementwise[LessThan[1]][{1, 0, 2, -1}]Elementwise[If[#1^2 >= #2^2, #1, #2]&][(| | | |
| -- | -- | -- |
| -9 | -5 | -7 |
| -2 | -4 | 1 |
| -3 | -8 | -4 |), (| | | |
| -- | -- | -- |
| -1 | 2 | -5 |
| 5 | -2 | -3 |
| 4 | 6 | 3 |)]Elementwise[StringJoin]["<", RandomWord[5], ">"]范围 (5)
默认情况下,Greater 不会遍历列表:
Greater[{1, -1, 3, -4, 5}, 0]用 Elementwise[Greater] 使其变为可列函数:
Elementwise[Greater][{1, -1, 3, -4, 5}, 0]Min 返回参数中最小的值:
Min[2, {-4, 2}, {{8, 5}, {-1, 6}}]用 Elementwise[Min] 使其能作用到最深层的元素:
Elementwise[Min][2, {-4, 1}, {{8, 5}, {0, 6}}]Block[{min},
SetAttributes[min, Listable];
min[2, {-4, 1}, {{8, 5}, {0, 6}}]
]下面是由日期组成的 TabularColumn 对象:
TabularColumn[RandomDate[10 ^ 5]]RepeatedTiming[mds = Elementwise[DateValue[#, {"Month", "Day"}]&][%]]用这两列数据构建 Tabular 对象:
ToTabular[{"month" -> mds[[All, 1]], "day" -> mds[[All, 2]]}, "Columns"]取一个包含两列三元组的 Tabular 对象:
tab = Tabular[{{{1, 2, 3}, 2}, {{2, -1, 4}, 1}}, {"vector", "scalar"}]TransformColumns[tab, "comp" -> Function[Elementwise[Greater][#vector, #scalar]]]Elementwise 可与 Threaded 一起使用:
Elementwise[f][{{1, 2, 3}, {4, 5, 6}}, {a, b}]Elementwise[f][{{1, 2, 3}, {4, 5, 6}}, Threaded[{α, β, γ}]]应用 (2)
firstNames = TabularColumn[{"Pierce", "Sean", "Daniel", "Timothy", "George", "Roger", "David"}];
lastNames = TabularColumn[{"Brosnan", "Connery", "Craig", "Dalton", "Lazenby", "Moore", "Niven"}];Elementwise[StringJoin][firstNames, " ", lastNames]Normal[%]dates = EntityValue[Entity["Person", "ErnestHemingway::8gktg"][EntityProperty["Person", "NotableBooks"]], EntityProperty["Book", "FirstPublished"]]Elementwise[LessEqualThan[Entity["Person", "ErnestHemingway::8gktg"]["DeathDate"]]][dates]//Counts属性和关系 (6)
Elementwise 遍历的方式与 Listable 函数类似:
Elementwise[f][1, {2, 3}, {{4, 5, 6}, {7, 8, 9}}]Block[{f},
SetAttributes[f, Listable];
f[1, {2, 3}, {{4, 5, 6}, {7, 8, 9}}]
]%% === %Elementwise[f][] 返回 f[]:
Elementwise[f][]Elementwise[f][{}] 返回 {}:
Elementwise[f][{}]对于几个长度相同的列表,Elementwise[f][list1,list2,…] 等价于 MapThread[f][{list1,list2,…}]:
Elementwise[f][{a, b}, {1, 2}, {x, y}]MapThread[f][{{a, b}, {1, 2}, {x, y}}]如果任何列表包含子列表,Elementwise 将尽可能遍历所有元素:
Elementwise[f][{{a}, {b}}, {1, 2}, {x, y}]MapThread[f][{{{a}, {b}}, {1, 2}, {x, y}}]另一个区别是 Elementwise 可遍历标量:
Elementwise[f][{a, b}, 0, {x, y}]如果 f[list,scalar] 没有计算,Elementwise[f][list,scalar] 等价于 Thread[f[list,scalar]]:
Elementwise[f][{a, b, c}, x]Thread[f[{a, b, c}, x]]也可用 Map 实现 Elementwise 的一些功能:
Elementwise[Max][{1, -1, 3, -4, 5}, 0]Map[Max[#, 0]&, {1, -1, 3, -4, 5}]或 Thread:
Elementwise[LessThan[1]][{1, 0, 2, -1}]Thread[LessThan[1][{1, 0, 2, -1}]]Elementwise[f][{{1, 2, 3}, {4, 5, 6}}, {a, b}]MapThread[Thread[f[#1, #2]]&, {{{1, 2, 3}, {4, 5, 6}}, {a, b}}]Elementwise 等价于 MapAt 在所有适用层级上使用 All:
data = {{0.2, 0.8, .9}, {0.12, .74}, {.4}, {.32, .85}};Elementwise[f][data]MapAt[f, data, {All, All}]文本
Wolfram Research (2025),Elementwise,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Elementwise.html.
CMS
Wolfram 语言. 2025. "Elementwise." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/Elementwise.html.
APA
Wolfram 语言. (2025). Elementwise. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Elementwise.html 年
BibTeX
@misc{reference.wolfram_2026_elementwise, author="Wolfram Research", title="{Elementwise}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/Elementwise.html}", note=[Accessed: 16-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_elementwise, organization={Wolfram Research}, title={Elementwise}, year={2025}, url={https://reference.wolfram.com/language/ref/Elementwise.html}, note=[Accessed: 16-June-2026]}