Convert horizontal array to vertical one in Julia

Viewed 479

assume I have a 3-element array like this:

A = [1, 2, 3]

How can I convert it to:

B = [1; 2; 3]

in Julia?

1 Answers

transpose creates an Adjoint object that actually holds a view

julia> A = [1, 2, 3];

julia> A'
1×3 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:
 1  2  3

Wrapping it with collect would make an actual horizontal vector:

julia> collect(A')
1×3 Array{Int64,2}:
 99  2  3

You need to however understand that in Julia 1-dimensional Vectors are always vertical while a vector can be also represented by a two-dimensional matrix where one dimension is equal to one:

julia> B=[1 2 3]
1×3 Array{Int64,2}:
 1  2  3

julia> C = collect(B')
3×1 Array{Int64,2}:
 1
 2
 3

Finally, each of those can be converted to a standar vector using the vec function or [:] operator:

julia>  A == B[:] == C[:] == vec(B) == vec(C) == @view(C[:]) == @view(B[:])
true
Related