Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia Initialized Array / Vector is not zero but random

When I define an Array in Julia:

z = Array(Float64, 1)

it appears a random value is assigned. Sometimes it is 0.0, but mostly it is something like 3.78692e-316.

Is this behaviour intended?

And how do I initialize a "constant" vector with 10 values, such as b = [2.0 2 2 2]?

like image 954
user2546346 Avatar asked May 28 '14 06:05

user2546346


1 Answers

@waTeim is correct that when allocating an array it is not initialized to 0 or any specific value.

The way to allocate and initialize a new array with a specific value in Julia is with fill() so for your b you would do:

b = fill(2.0, 10)

This gives you:

10-element Array{Float64,1}:
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0

Or if you want a row vector:

b = fill(2.0, 1, 10)

Which gives you

1x10 Array{Float64,2}:
 2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0
like image 128
Mr Alpha Avatar answered Sep 27 '22 17:09

Mr Alpha