Create an array using the output of size()

Matlab:

a =

   -1.3077    3.5784    3.0349
   -0.4336    2.7694    0.7254
    0.3426   -1.3499   -0.0631

>> zeros(size(a))

ans =

     0     0     0
     0     0     0
     0     0     0

julia> a
3×3 Matrix{Float64}:
 -0.348874   0.581243   1.20375
 -1.13732    0.943546  -0.634119
 -1.85663   -1.34655    0.521267

julia> Int32[size(a)]
ERROR: MethodError: Cannot `convert` an object of type Tuple{Int64, Int64} to an object of type Int32

julia> Int32(size(a))
ERROR: MethodError: no method matching Int32(::Tuple{Int64, Int64})
julia> a = rand(3, 3)
3×3 Matrix{Float64}:
 0.893835  0.365916   0.921844
 0.971955  0.0643828  0.63403
 0.149759  0.247479   0.0915574

julia> zeros(size(a))
3×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0

julia> zeros(Int, size(a))
3×3 Matrix{Int64}:
 0  0  0
 0  0  0
 0  0  0

:slight_smile:

3 Likes

In case you don’t care of the contents of the produced array

julia> a = rand(3, 3)
3×3 Matrix{Float64}:
 0.305883  0.416786  0.22493
 0.777658  0.645797  0.475424
 0.597531  0.145924  0.909268

julia> similar(a, Int)
3×3 Matrix{Int64}:
 140660108426576  140660108426976  140660101478000
 140660101477712  140660108427056  140660108443664
 140660108426896  140660108427136  140660108426816
4 Likes

Another syntax that is found frequently, but do not know the pros/cons:

Array{Int}(undef,size(a))
1 Like
julia> zero(a)
3×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0
3 Likes