gpt4 book ai didi

indexing - Julia - 如何用索引向量的向量来索引向量?

转载 作者:行者123 更新时间:2023-12-05 00:52:09 26 4
gpt4 key购买 nike

假设 idxs 是定义为的向量的向量

3-element Vector{Vector{Int64}}:
[1, 2]
[1, 3]
[1, 4]

假设 A 是一个向量,定义为

5-element Vector{Int64}:
6
7
8
9
10

使用 Python 和 numpy,我可以简单地(在伪代码中)A[idxs] 这将返回一个包含元素的二维数组

[
[6 7]
[6 8]
[6 9]
]

如何在 Julia 中执行相同的索引,在其中我多次索引到一维向量中,并检索条目矩阵?

谢谢

最佳答案

您可以将数组推导式与 hcat 一起使用。您可以使用生成的 2x3 矩阵或转置结果。

julia> hcat([A[i] for i in id]...)'
3×2 adjoint(::Matrix{Int64}) with eltype Int64:
6 7
6 8
6 9

数据

julia> show(id)
[[1, 2], [1, 3], [1, 4]]

julia> show(A)
[6, 7, 8, 9, 10]

关于indexing - Julia - 如何用索引向量的向量来索引向量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70636039/

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