Ho can I append a value into initialised array without first empty row?

Hi,

I am trying to append some values into an initialized array. But first elements of this array is not the first appended value. I did like follows:

#initialized an array
a = Array{Float64,1}
#append into a
a = [a;5;6]
print(a)

The output is as follows:

2-element Array{Any,1}:
  Array{Float64,1}
 5
 6

But I don’t need Array{Float64,1} as my first element? I just need an array with two elements

2-element Array{Any,1}:
 5
 6

I can do it by using a[2:end] but is there any other method to solve this issue?

a = Array{Float64,1} doesn’t do what you think it does, as it assigns the type Array{Float64,1} to a. What you’re probably looking for is a = Array{Float64,1}(), so you’re actually calling the constructor. A shorter way to achive this would also be a = Float64[]. Are you also sure that you want to use the non-inplace vcat (eg. [a; b])? You might want to have a look at push! or append!.

2 Likes

@simeonschaub: Thanks a lot for your reply. I am a beginner and still, I have some confusions with variable initializations because I just moved from MATLAB to julia.