Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JULIA how do I preallocate a Measurements.jl vector with n rows?

Tags:

julia

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)
like image 872
Rod Avatar asked Dec 19 '25 21:12

Rod


1 Answers

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).

like image 157
cbk Avatar answered Dec 22 '25 17:12

cbk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!