Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best way to convert Vector into Matrix in Julia?

Suppose I have a variable v of a type Vector.

What would be the best / fastest way to just convert it into Matrix representation (for whatever reason)?

To clarify, v'' will do the job, but is it the best way to do this?

like image 804
aberdysh Avatar asked Jan 04 '16 16:01

aberdysh


People also ask

What are vectors in Julia?

For Julia, Vectors are just a special kind of Matrix, namely with just one row (row matrix) or just one column (column matrix): Julia Vectors can come in two forms: Column Matrices (one column, N rows) and Row Matrices (one row, N columns) Very useful operation. Takes any collection and returns a representation containing n rows and m columns:

What is the difference between matrices and vectors?

Matrices are probably one of the data structures you'll find yourself using very often. For Julia, Vectors are just a special kind of Matrix, namely with just one row (row matrix) or just one column (column matrix): Julia Vectors can come in two forms: Column Matrices (one column, N rows) and Row Matrices (one row, N columns)

How do you find the vector of a matrix?

A vector, like →x is usually identified with the n × 1 matrix (a column vector). Were that done, the system of equations would be written Mx = b. If we refer to a matrix M by its components, a convention is to use (M)ij or mij to denote the entry in the i th row and j th column.

How do you separate elements of a vector in Julia?

Separate its elements by commas. For many purposes, though, an n -vector in Julia is a lot like an n × 1 column vector. Concatenated elements within brackets may be matrices for a block representation, as long as all the block sizes are compatible.


2 Answers

Reshape should be the most efficient. From the docs:

reshape(A, dims): Create an array with the same data as the given array, but with different dimensions. An implementation for a particular type of array may choose whether the data is copied or shared.

julia> v = rand(3)
3-element Array{Float64,1}:
 0.690673 
 0.392635 
 0.0519467

julia> reshape(v, length(v), 1)
3x1 Array{Float64,2}:
 0.690673 
 0.392635 
 0.0519467
like image 84
Chisholm Avatar answered Sep 18 '22 16:09

Chisholm


v[:,:] is probably the clearest way to do this.

For example:

julia> v=[1,2,3]
3-element Array{Int64,1}:
1
2
3

julia> m=v[:,:]
3x1 Array{Int64,2}:
1
2
3

julia> ndims(m)
2
like image 37
aberdysh Avatar answered Sep 17 '22 16:09

aberdysh