Julia预分配数组与MATLAB预分配数组

时间:2019-05-06 20:11:00

标签: julia

在MATLAB中,建议对数组进行预分配,否则将在迭代过程中更改大小。假设该建议也对朱莉娅有所帮助,我想知道如何做到这一点。

在MATLAB中,以下代码预分配了一个5 x 10的数组:

A = nan(5,10)

在朱莉娅中如何获得相同的东西?

2 个答案:

答案 0 :(得分:8)

A = nan(5,10)不仅分配了double s的数组,而且还用NaN s初始化了数组的条目(尽管MATLAB可能无法真正填充数组的内容)

简短的答案是:在MATLAB中,A = nan(5, 10)与在Julia中的A = fill(NaN, 5, 10)在语义上是

长答案是,您可以在Julia中为数组分配和初始化提供许多选择和更多控制权。

数组分配没有初始化

在Julia中,可以分配一个数组或一个矩阵(它是一个2D数组),而无需初始化条目。

# Allocate an "uninitialized" m-by-n `Float64` (`double`) matrix
A = Array{Float64, 2}(undef, m, n)
# or equivalently
A = Matrix{Float64}(undef, m, n) # `Matrix{T}` is equivalent to `Array{T, 2}`
# you do not need to type dimensionality even with `Array`,
# the dimensionality will be inferred from the number of parameters
A = Array{Float64}(undef, m, n)

# You can do the same for arrays of different dimensions or other types
A = Array{Float64, 3}(undef, m, n, k) # 3D `Float64` array of size m*n*k
A = Array{Int64}(undef, m) # 1D `Int64` array
A = Vector{Float32}(undef, m) # 1D `Float32` (i.e. `single`) array. `Vector{T} === Array{T, 1}`

无需使用其他数组进行初始化的数组分配

在Julia中,您可以使用函数similar使用另一个矩阵的类型,元素类型和维数信息分配数组,而无需初始化。

A = zeros(UInt8, m, n)
B = similar(A) # allocates the same type of array (dense, sparse, etc.) with the same element type, and the same dimensions as `A`

C = similar(A, Float64) # allocates the same type of array with the same dimensions as `A` but with the element type of `Float64`

分配一个空数组

您可以使用以上传递0作为维度的数组构造语法,也可以简单地使用T[]创建类型为T的空数组。

A = Float64[]

具有初始化的数组分配

# Allocate a `Float64` array and fill it with 0s
A = zeros(m, n) # m-by-n Float64 matrix filled with zeros
A = zeros(m, n, k, l) # a 4D Float64 array filled with zeros

# similarly to fill with `Float64` 1s
A = ones(m, n)
A = ones(m) # a 1D array of size `m`
A = ones(m, 1) # an `m`-by-1 2D array

# you can use these functions with other types as well
A = zeros(Float32, m, n)
A = ones(UInt8, m, n, k)


# you can allocate an array/matrix and fill it with any value you like using `fill`
# the type is inferred by the value entered
A = fill(4.0, (m, n)) # m-by-n matrix filled with `4.0`
A = fill(0.50f, m, n, k) # a 3D Float32 array filled `0.5`s

# so to fill with `NaN`s you can use
A = fill(NaN, m, n)

# random initialization
A = rand(m, n) # m-by-n Float64 matrix with uniformly distributed values in `[0,1)`
A = rand(Float32, m) # an array of size `m` with uniformly distributed values in `[0,1)`

A = randn(m, n) # the same as `rand` but with normally distributed values

# you can initialize the array with values randomly (uniform) picked from a collection
A = rand([1, 5, 7], m, n) # values will be picked from the array `[1,5,7]`

您可以使用fill!(A, value)或简单地使用A .= value来用相同的值填充已经分配的数组。如果导入模块Random,则可以使用rand!randn!用随机值填充已经分配的数组。由于可以避免分配,因此这可能会给您带来显着的性能优势。

您可以查看Julia文档的the Multi-dimensional Arrays部分,以了解有关Julia中数组的更多信息。


注释

在Julia中,您无法更改内置的 Array多维(不是1D)尺寸。

A = zeros(5,5)
A[6,5] = 2 # bounds error

但是您可以将push!的值转换成一维 Array。这样可以有效地调整数组的大小。

julia> A = Int[];
julia> push!(A, 1);
julia> push!(A, 2, 3);
julia> A
3-element Array{Int64,1}:
 1
 2
 3

答案 1 :(得分:2)

在MATLAB中,nan()将NaN数组分配为浮点值。所以在朱莉娅

 A = fill(NaN, (5,10))

这样做。