朱莉娅多维数组类型? [英] Julia multidimensional array types?
问题描述
我有一个类型为Array{Float64,2}
的数组,但是它是1列的数组,因此我无法将其传递给期望类型为Array{Float64,1}
的单列数组的函数.我真的不了解2
的含义或如何解决我的问题,而且我也无法通过搜索任何文档来弄清问题.
在Array{Float64,2}
中,2是数组中的维数.由于您说的是它是由1列组成的数组",因此您可能具有二维的内容,其中包含一行或一列,即其中一个
julia> c = rand(1,3)
1x3 Array{Float64,2}:
0.190944 0.928697 0.251519
julia> d = rand(3,1)
3x1 Array{Float64,2}:
0.0818493
0.0342291
0.58341
要将其转换为一维数组,可以手动切片该数组或使用 解决方案
In Array{Float64,2}
, the 2 is the number of dimensions in the array. Since you say it's "it's an array of 1 column", you probably have something which is 2-dimensional with either one row or one column, i.e. one of
julia> c = rand(1,3)
1x3 Array{Float64,2}:
0.190944 0.928697 0.251519
julia> d = rand(3,1)
3x1 Array{Float64,2}:
0.0818493
0.0342291
0.58341
To turn this into a 1-dimensional array, you could slice the array manually or use squeeze
, as you prefer:
julia> c[1,:]
3-element Array{Float64,1}:
0.190944
0.928697
0.251519
julia> squeeze(d,2)
3-element Array{Float64,1}:
0.0818493
0.0342291
0.58341
Either approach should give you something of type Array{Float64,1}
.
As noted in the comments, another approach is to use reshape
, e.g. (using a different random c
):
julia> reshape(c, length(c))
3-element Array{Float64,1}:
0.680653
0.0573147
0.607054
This has the advantage -- and disadvantage -- of not caring whether you have an array of shape 1xN or Nx1.
这篇关于朱莉娅多维数组类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!