gpt4 book ai didi

arrays - 如何在 Julia 中对可变数量的列表进行笛卡尔积?

转载 作者:行者123 更新时间:2023-12-02 18:32:12 25 4
gpt4 key购买 nike

对于集合 {1, 2, ..., n} 中的每个值 j,其中 n 的值可以变化(它是我的程序中的某个变量,可以根据用户的输入而不同),我有一个数组 A_j。我想获得所有数组 A_j 的笛卡尔积,以便我可以迭代该笛卡尔积(从每个 A_1、A_2、... A_n 中取出一个元素以获得一个元组 (a_1、a_2、... , a_n) 在 A_1 x A_2 x ... x A_n) 中。我如何在 Julia 中实现这一目标?

最佳答案

使用Iterators.product :

help?> Iterators.product
product(iters...)

Return an iterator over the product of several iterators. Each generated
element is a tuple whose ith element comes from the ith argument iterator.
The first iterator changes the fastest.

Examples
≡≡≡≡≡≡≡≡≡≡

julia> collect(Iterators.product(1:2, 3:5))
2×3 Matrix{Tuple{Int64, Int64}}:
(1, 3) (1, 4) (1, 5)
(2, 3) (2, 4) (2, 5)

关于arrays - 如何在 Julia 中对可变数量的列表进行笛卡尔积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69277451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com