Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert horizontal array to vertical one in Julia

Tags:

julia

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?

like image 205
weera Avatar asked Dec 22 '20 19:12

weera


People also ask

How do you reshape an array in Julia?

Reshaping array dimensions in Julia | Array reshape() Method The reshape() is an inbuilt function in julia which is used to return an array with the same data as the specified array, but with different specified dimension sizes.

Are arrays mutable in Julia?

Arrays in Julia are mutable and hence it allows modification of its content. Elements in arrays can be removed or updated as per requirement.

How do you fill an array in Julia?

Create an array filled with the value x . For example, fill(1.0, (10,10)) returns a 10x10 array of floats, with each element initialized to 1.0 . If x is an object reference, all elements will refer to the same object. fill(Foo(), dims) will return an array filled with the result of evaluating Foo() once.

How do you make a vector in Julia?

Creating a Vector A Vector in Julia can be created with the use of a pre-defined keyword Vector() or by simply writing Vector elements within square brackets([]). There are different ways of creating Vector. vector_name = [value1, value2, value3,..] or vector_name = Vector{Datatype}([value1, value2, value3,..])


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
like image 52
Przemyslaw Szufel Avatar answered Jan 04 '23 08:01

Przemyslaw Szufel