Julia: append to an empty vector
Asked Answered
W

5

53

I would like to create an empty vector and append to it an array in Julia. How do I do that?

x = Vector{Float64}
append!(x, rand(10))

results in

`append!` has no method matching append!(::Type{Array{Float64,1}}, ::Array{Float64,1})

Thanks.

Wesla answered 15/2, 2015 at 8:2 Comment(0)
S
57

Your variable x does not contain an array but a type.

x = Vector{Float64}
typeof(x)  # DataType

You can create an array as Array(Float64, n) (but beware, it is uninitialized: it contains arbitrary values) or zeros(Float64, n), where n is the desired size.

Since Float64 is the default, we can leave it out. Your example becomes:

x = zeros(0)
append!( x, rand(10) )
Sverdlovsk answered 15/2, 2015 at 8:57 Comment(1)
How do you extend this to a 2D array? For example I have a number of type Array{Float64,2} that I want to append to a blank array of the same type but I'm gettting the error 'ERROR: MethodError: append! has no method matching append!(::Array{Float64,2}, ::Array{Float64,2})'.Regorge
R
50

I am somewhat new to Julia and came across this question after getting a similar error. To answer the original question for Julia version 1.2.0, all that is missing are ():

x = Vector{Float64}()
append!(x, rand(10))

This solution (unlike x=zeros(0)) works for other data types, too. For example, to create an empty vector to store dictionaries use:

d = Vector{Dict}()
push!(d, Dict("a"=>1, "b"=>2))

A note regarding use of push! and append!:

According to the Julia help, push! is used to add individual items to a collection, while append! adds an collection of items to a collection. So, the following pieces of code create the same array:

Push individual items:

a = Vector{Float64}()
push!(a, 1.0)
push!(a, 2.0)

Append items contained in an array:

a = Vector{Float64}()
append!(a, [1.0, 2.0])
Ruebenrueda answered 7/10, 2019 at 19:4 Comment(1)
Thanks for this answer! It was indeed the difference between append! and push! that was tripping me up :)Augusto
B
15

You can initialize an empty Vector of any type by typing the type in front of []. Like:

Float64[] # Returns what you want
Array{Float64, 2}[] # Vector of Array{Float64,2}
Any[] # Can contain anything
Bland answered 15/2, 2015 at 11:52 Comment(0)
H
8

New answer, for Julia 1. append! is deprecated, you now need to use push!(array, element) to add elements to an array

my_stuff = zeros()
push!(my_stuff, "new element")
Helminthology answered 28/11, 2018 at 17:23 Comment(3)
Running your code example results in error: julia> my_stuff = zeros(); push!(my_stuff, "new element") ERROR: MethodError: no method matching push!(::Array{Float64,0}, ::String) Closest candidates are: push!(::Any, ::Any, ::Any) at abstractarray.jl:2064 push!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:2065 push!(::Array{Any,1}, ::Any) at array.jl:862 ... Stacktrace: [1] top-level scope at none:0Antisana
@Antisana are you on Julia 1.0? I'm not sure this will work the same on other versions, especially earlier versionsHelminthology
did you try running the code yourself? zeros() create an array of type Float and you are trying to add strings to Float. My version is 1.1.1. and you can see the error in code also here online.Antisana
W
0

A summary of various possibilities to create an empty float array and to "append":

x = Float64[] 
x = zeros()
x = zeros(0)
x = ones()
x = ones(0)
x = [0.][1:0]

a = [0., 10.]

append!(x, a)
push!(x, a...)
x = [x..., a...]
x = vcat(x, a)
Wanigan answered 20/4, 2024 at 12:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.