The code below creates three identical matrices, m1, m2, and m3 in three
different ways. m3 was created programatically. The function foo could
be anything the user would like it to be, subject to converting an Int into a Float64.
m1 = [1.0 2.0]
typeof(m1) # Matrix{Float64} (alias for Array{Float64, 2})
m2 = Array{Float64}(undef, (1, 2))
m2[1,1] = 1.0
m2[1,2] = 2.0
typeof(m2) # Matrix{Float64} (alias for Array{Float64, 2})
m1 == m2 # true
typeof(m1) == typeof(m2) # true
m3 = Array{Float64}(undef, (1, 2))
function foo(i::Int)::Float64
return Float64(i)
end
for i in 1:2
m3[1, i] = foo(i)
end
typeof(m3) # Matrix{Float64} (alias for Array{Float64, 2})
m1 == m3 # true
typeof(m1) == typeof(m3) # true
One alternative way to create a matrix like m1 would be to apply
a function to the vector v defined below. Of course it is easy to write
such a function. But it could already exist and be fast. Does such a
function exist in Base or in some package?
Iām fairly certain that your succinct version allocates a new matrix entirely, which the reshape version does not. May be relevant for someone doing that in a tight loop!