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?
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:
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)
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With