`map` 相当于二维列表理解
`map` equivalent to 2d list comprehension
在 1d 中,我可以使用其中之一:
[i for i in 1:5]
或
map(1:5) do i
i
end
都生产
[1,2,3,4,5]
有没有办法在更高的维度上使用 map
?例如复制
[x + y for x in 1:5,y in 10:13]
产生
5×4 Array{Int64,2}:
11 12 13 14
12 13 14 15
13 14 15 16
14 15 16 17
15 16 17 18
你可以这样做:
julia> map(Iterators.product(1:3, 10:15)) do (x,y)
x+y
end
3×6 Array{Int64,2}:
11 12 13 14 15 16
12 13 14 15 16 17
13 14 15 16 17 18
你写的理解我觉得只是collect(x+y for (x,y) in Iterators.product(1:5, 10:13))
,.注意方括号 (x,y)
,因为 do
函数获取一个元组。不像 x,y
当它有两个参数时:
julia> map(1:3, 11:13) do x,y
x+y
end
3-element Array{Int64,1}:
12
14
16
这当然不是您正在寻找的 map
等价物,但在某些情况下,您可以使用带有矢量和转置矢量的广播:
x = 1:5
y = (10:13)'
x .+ y
在 REPL:
julia> (1:5) .+ (10:13)'
5×4 Array{Int64,2}:
11 12 13 14
12 13 14 15
13 14 15 16
14 15 16 17
15 16 17 18
在 1d 中,我可以使用其中之一:
[i for i in 1:5]
或
map(1:5) do i
i
end
都生产
[1,2,3,4,5]
有没有办法在更高的维度上使用 map
?例如复制
[x + y for x in 1:5,y in 10:13]
产生
5×4 Array{Int64,2}:
11 12 13 14
12 13 14 15
13 14 15 16
14 15 16 17
15 16 17 18
你可以这样做:
julia> map(Iterators.product(1:3, 10:15)) do (x,y)
x+y
end
3×6 Array{Int64,2}:
11 12 13 14 15 16
12 13 14 15 16 17
13 14 15 16 17 18
你写的理解我觉得只是collect(x+y for (x,y) in Iterators.product(1:5, 10:13))
,.注意方括号 (x,y)
,因为 do
函数获取一个元组。不像 x,y
当它有两个参数时:
julia> map(1:3, 11:13) do x,y
x+y
end
3-element Array{Int64,1}:
12
14
16
这当然不是您正在寻找的 map
等价物,但在某些情况下,您可以使用带有矢量和转置矢量的广播:
x = 1:5
y = (10:13)'
x .+ y
在 REPL:
julia> (1:5) .+ (10:13)'
5×4 Array{Int64,2}:
11 12 13 14
12 13 14 15
13 14 15 16
14 15 16 17
15 16 17 18