sách gpt4 ai đã đi

Cách chuyển đổi từ Array{Array{Int64,2},1} sang Array{Int64,2}

In lại 作者:行者123 更新时间:2023-12-01 08:50:51 27 4
mua khóa gpt4 Nike

在 Julia 中,我想将定义为二维数组向量的数据转换为二维矩阵数组。
如下例所述,我想把数据s转换成数据t,但是至今没有成功。
我该如何处理这个案子?

julia> s = [[1 2 3], [4 5 6], [7 8 9]]
3-element Array{Array{Int64,2},1}:
[1 2 3]
[4 5 6]
[7 8 9]

julia> t = [[1 2 3]; [4 5 6]; [7 8 9]]
3××3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9

julia> s |> typeof
Array{Array{Int64,2},1}

julia> t |> typeof
Array{Int64,2}

julia> convert(Array{Int64, 2}, s)
ERROR: MethodError: Cannot `convert` an object of type Array{Array{Int64,2},1} to an object of type Array{Int64,2}
This may have arisen from a call to the constructor Array{Int64,2}(...),
since type constructors fall back to convert methods.

julia> reshape(s, 3, 3)
ERROR: DimensionMismatch("new dimensions (3,3) must be consistent with array size 3")
in reshape(::Array{Array{Int64,2},1}, ::Tuple{Int64,Int64}) at .\array.jl:113
in reshape(::Array{Array{Int64,2},1}, ::Int64, ::Int64, ::Vararg{Int64,N}) at .\reshapedarray.jl:39

在下面的例子中,如果你将二维数组或一维数组定义为源数据,那么我可以成功地将它们 reshape 为矩阵的二维数组。
julia> u = [1 2 3 4 5 6 7 8 9]
1××9 Array{Int64,2}:
1 2 3 4 5 6 7 8 9

julia> u |> typeof
Array{Int64,2}


julia> reshape(u, 3, 3)
3××3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9



julia> v = [1, 2, 3, 4, 5, 6, 7, 8, 9]
9-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9

julia> v |> typeof
Array{Int64,1}

julia> reshape(v, 3, 3)
3××3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9

1 Câu trả lời

您可以使用 vcat 和 splatting

julia> t = vcat(s...)
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9

关于arrays - 如何从 Array{Array{Int64,2},1} 转换为 Array{Int64,2},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43536746/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com