Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending to Vector

Tags:

julia

I'm having trouble appending to an empty vector in Julia.

v = Int64[]
append!(v,1)
append(v,1)

The append! gives the error

ERROR: `Variable` has no method matching Variable(::Int64, ::Int64, ::Int64, ::Int64)

And append gives the error

ERROR: append not defined

This is probably a basic mistake on my part, but I can't figure out why neither command is working.

like image 261
Rob Donnelly Avatar asked Feb 19 '15 08:02

Rob Donnelly


People also ask

How do you add items to a vector in C++?

To add elements to vector, you can use push_back() function. push_back() function adds the element at the end of this vector.

How do I append one STD to another vector?

Use the insert Function to Append Vector to Vector in C++ The insert method is a built-in function of the std::vector container that can add multiple elements to the vector objects.

How do I append a vector to another vector in R?

To append one vector to another in R, call append() function and pass the two vectors as arguments. append() returns a new vector with the elements of first vector appended with that of second vector.

Can we append two vectors?

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.


1 Answers

If you're appending a scalar value, you want push!. If you're adding a list of elements, then you want append!. There's a good reason for the distinction, as you will probably realize if you consider what should happen if you want to build an array-of-arrays.

Typing ?append! at the REPL will show you help on the function, including a demo on how to use it. (In julia 0.4, the help has been improved and refers you to the push! function as well, but that doesn't seem to have been implemented in the current release.)

like image 165
tholy Avatar answered Sep 22 '22 17:09

tholy