【问题标题】:Vectorization of different dimension不同维度的向量化
【发布时间】:2021-03-18 12:29:32
【问题描述】:

我想将 1:3 的所有排列作为字符串。

julia> string.(permutations(1:3)...) # 1:3 is for the example, the real problem is larger
3-element Vector{String}:
 "112233"
 "231312"
 "323121"

但是结果是我想要的“转置”

6-element Vector{String}:
 "123"
 "132"
 "213"
 "231"
 "312"
 "321"

这个向量将是其他一些(向量化)函数调用F.(perms) 的输入,我想有效地做到这一点。 我该怎么做?

【问题讨论】:

  • 广播,你称之为“矢量化”,与其说是效率,不如说是语义。我认为string.(permutations(1:3)) 应该会给你正确的结果,但这并不比等效循环更有效。
  • 这没有给出正确的答案,string.(zip(permutations(1:3)...)...) 有效,或者只是把它放在一个循环中。但是非常感谢您对效率的关注,我现在实际上意识到手写循环与嵌套点/矢量化函数调用一样有效。我不知何故认为嵌套的点调用效率更高。

标签: julia vectorization


【解决方案1】:

只要做:

julia> join.(permutations(1:3),"")
6-element Vector{String}:
 "123"
 "132"
 "213"
 "231"
 "312"
 "321"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    相关资源
    最近更新 更多