Home » Julia » How to Create Vector in Julia?

# How to Create Vector in Julia?

In Julia, a vector is a one-dimensional array of elements that can be accessed by their index. Vectors are commonly used to store and manipulate data in scientific and mathematical computing. There are several ways to create a vector in Julia. The most common way is to use the collect function, which takes an iterable object (such as an array or a range) as an argument and returns a vector with the same elements. Below are some examples:

## Create Vector in Julia Examples

```julia> collect(1:5)
5-element Array{Int64,1}:
1
2
3
4
5```

In this example, the `collect` function is used to create a vector with the elements `1`, `2`, `3`, `4`, and `5`. The `1:5` syntax is used to create a range of numbers from `1` to `5`, which is then passed as an argument to the `collect` function.

## Using vec Function in Julia

Another way to create a vector in Julia is to use the `vec` function, which is similar to `collect` but returns a vector of type `Vector` rather than a regular array. For example:

```julia> vec(1:5)
5-element Vector{Int64}:
1
2
3
4
5```

In this example, the `vec` function is used to create a vector with the elements `1`, `2`, `3`, `4`, and `5`. As you can see, the `vec` function produces the same result as the `collect` function, but the resulting vector is of type `Vector` rather than a regular array.

## Using the Vector Function in Julia

You can also use the `Vector` function to create a vector in Julia. This function takes a list of elements as an argument and returns a vector with the same elements. For example:

```julia> Vector([1, 2, 3, 4, 5])
5-element Vector{Int64}:
1
2
3
4
5```

In this example, the `Vector` function is used to create a vector with the elements `1`, `2`, `3`, `4`, and `5`. The list of elements is passed as an argument to the `Vector` function, which returns a vector with the same elements.

## Using the fill Function in Julia

You can also use the `fill` function to create a vector of a specific size with the same element repeated. For example:

```julia> fill(1, 5)
5-element Array{Int64,1}:
1
1
1
1
1

julia> fill(1, 5, vec)
5-element Vector{Int64}:
1
1
1
1
1

julia> fill(1, 5, Vector)
5-element Vector{Int64}:
1
1
1
1
1

julia> fill(1, 5, Vector{Int64})
5-element Vector{Int64}:
1
1
1
1
1```

In these examples, the `fill` function is used to create vectors of size 5 with the element `1` repeated. The `fill` function takes the element to repeat as the first argument and the size of the vector as the second argument. You can also specify a vector constructor as the third argument to control the type of the resulting vector.

Here are a few more examples of creating vectors with random elements in Julia:

```julia> rand(5)
5-element Array{Float64,1}:
0.572428
0.691394
0.591458
0.826224
0.209213

julia> rand(5, vec)
5-element Vector{Float64}:
0.687733
0.749905
0.413824
0.914159
0.250619

julia> rand(5, Vector)
5-element Vector{Float64}:
0.768625
0.454217
0.586898
0.0247973
0.998432

julia> rand(5, Vector{Int64})
5-element Vector{Int64}:
0
0
0
0
0```

In these examples, the `rand` function is used to create vectors with five random elements. The `rand` function takes the size of the vector as an argument and returns a vector with random elements of type `Float64`. You can also specify a vector constructor as the second argument to control the type of the resulting vector.

For example, in the fourth example above, the `rand` function is used to create a vector of type `Vector{Int64}` with five random elements. Since the elements of the vector are of type `Int64`, the random elements are rounded to the nearest integer.