Reputation: 129
I have this code:
A = Array{Float64,4}(undef, 2,1,1, 4)
and it gives me an array with size (2,1,1,4) with random numbers.
How I can initialize this array?
Upvotes: 8
Views: 7771
Reputation: 10984
You can use fill!
to fill it with whatever you want:
julia> A = Array{Float64,2}(undef, 2, 3)
2×3 Array{Float64,2}:
6.93727e-310 6.93727e-310 6.93727e-310
6.93728e-310 6.93727e-310 0.0
julia> fill!(A, 42.0);
julia> A
2×3 Array{Float64,2}:
42.0 42.0 42.0
42.0 42.0 42.0
Note that you could have used fill
directly:
julia> fill(42.0, 2, 3)
2×3 Array{Float64,2}:
42.0 42.0 42.0
42.0 42.0 42.0
and if you want it zeroed (which is pretty common) you can use zeros
:
julia> zeros(2, 3)
2×3 Array{Float64,2}:
0.0 0.0 0.0
0.0 0.0 0.0
Upvotes: 7
Reputation: 14501
The code below produces an Array that is initialized with random values.
julia> rand(Int8, 2,1,1,4)
2×1×1×4 Array{Int8,4}:
[:, :, 1, 1] =
114
26
[:, :, 1, 2] =
-52
-96
[:, :, 1, 3] =
42
-53
[:, :, 1, 4] =
-106
47
See the Julia Docs for more about Array initialization.
Upvotes: 1