I want to preallocate a vector with n rows (e.g. A below) ready for filling in a loop with Measurements.jl variables, i.e. variables of the form a ± b. For example:
using Measurements
A = zeros(5,1) # this doesn't work
for n = 1:5
local B = rand() ± rand()
local C = rand() ± rand()
global A[n,1] = sqrt.((B-C).^2)
end
println(A)
You can make an array of Measurements.jl Measurements is several ways, including
A = fill(0±0, size)
or
A = Array{Measurement}(undef, size)
For example
using Measurements
A = fill(0±0, 5, 1)
for n = 1:5
B = rand() ± rand()
C = rand() ± rand()
A[n,1] = sqrt.((B-C).^2)
end
julia> A
5×1 Matrix{Measurement{Float64}}:
0.28 ± 1.0
0.45 ± 0.87
0.67 ± 0.58
0.28 ± 0.62
0.3 ± 0.67
N.B., you shouldn't generally need those global and local annotations here, unless you're running in global scope in a script (as opposed to in a REPL, in a function, a let block, or really anywhere else).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With