在Julia中将输入转换为数组

4

1
我没有听说过这样的函数,但你可以直接写:ndims(x) == 1 ? reshape(x, :, 1) : x - Bogumił Kamiński
@BogumiłKamiński 感谢您的建议,我会尝试一下。 - Mohammad Saad
1个回答

3

通过查看Python Numpy文档,这需要定义为:

atleast_2d(a) = fill(a,1,1)
atleast_2d(a::AbstractArray) = ndims(a) == 1 ? reshape(a, :, 1) : a

测试:

julia> atleast_2d(3)
1×1 Matrix{Int64}:
 3

julia> atleast_2d([4,5])
2×1 Matrix{Int64}:
 4
 5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接