# How do I transform a vector into a Matrix

**URL:** <https://discourse.julialang.org/t/how-do-i-transform-a-vector-into-a-matrix/85032>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 30, 2022, 3:07pm UTC](https://discourse.julialang.org/t/how-do-i-transform-a-vector-into-a-matrix/85032 "2022-07-30T15:07:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [July 30, 2022, 3:07pm UTC](https://discourse.julialang.org/t/how-do-i-transform-a-vector-into-a-matrix/85032/1 "2022-07-30T15:07:41Z")

</div>

The code below creates three identical matrices, `m1`, `m2`, and `m3` in three  
different ways. `m3` was created programatically. The function `foo` could  
be anything the user would like it to be, subject to converting an `Int` into a  
`Float64`.

```julia
m1 = [1.0 2.0]

typeof(m1) # Matrix{Float64} (alias for Array{Float64, 2})

m2 = Array{Float64}(undef, (1, 2))

m2[1,1] = 1.0

m2[1,2] = 2.0

typeof(m2) # Matrix{Float64} (alias for Array{Float64, 2})

m1 == m2 # true
typeof(m1) == typeof(m2) # true

m3 = Array{Float64}(undef, (1, 2))

function foo(i::Int)::Float64
    return Float64(i)
end    

for i in 1:2
    m3[1, i] = foo(i)
end

typeof(m3) # Matrix{Float64} (alias for Array{Float64, 2})

m1 == m3 # true
typeof(m1) == typeof(m3) # true

```

One alternative way to create a matrix like `m1` would be to apply  
a function to the vector `v` defined below. Of course it is easy to write  
such a function. But it could already exist and be fast. Does such a  
function exist in `Base` or in some package?

```julia
v1 = [1.0, 2.0]    

typeof(v1) # Vector{Float64} (alias for Array{Float64, 1})

typeof(v1) == typeof(m1) # false

# m4 = somefunction(v1)
# m4 == m1 true
# typeof(m4) == typeof(m1) true

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 30, 2022, 3:23pm UTC](https://discourse.julialang.org/t/how-do-i-transform-a-vector-into-a-matrix/85032/2 "2022-07-30T15:23:48Z")

</div>

Does this answer your question?

```julia
julia> m1 = [1.0 2.0]
1×2 Matrix{Float64}:
 1.0 2.0

julia> vec(m1)
2-element Vector{Float64}:
 1.0
 2.0

julia> reshape(m1, 2)
2-element Vector{Float64}:
 1.0
 2.0

```

Re-reading the title it seems you want to go the other way.

```julia
julia> v1 = [1.0, 2.0]
2-element Vector{Float64}:
 1.0
 2.0

julia> reshape(v1, (1,2))
1×2 Matrix{Float64}:
 1.0 2.0

julia> reshape(v1, (2,1))
2×1 Matrix{Float64}:
 1.0
 2.0

```

Also, you can broadcast foo with adding a `.`.

```julia
julia> v2 = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> foo.(v2)
3-element Vector{Float64}:
 1.0
 2.0
 3.0

julia> M2 = [1 2 3]
1×3 Matrix{Int64}:
 1 2 3

julia> foo.(M2)
1×3 Matrix{Float64}:
 1.0 2.0 3.0

```

---

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [July 30, 2022, 5:42pm UTC](https://discourse.julialang.org/t/how-do-i-transform-a-vector-into-a-matrix/85032/3 "2022-07-30T17:42:10Z")

</div>

Thanks. `reshape` solved my problem.

---

<div class="post-metadata">

**Author:** ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Post date:** [July 30, 2022, 11:49pm UTC](https://discourse.julialang.org/t/how-do-i-transform-a-vector-into-a-matrix/85032/4 "2022-07-30T23:49:03Z")

</div>

For lovers of succinctness:

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

julia> v[:,:]
3×1 Matrix{Int64}:
 1
 2
 3

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 31, 2022, 9:45am UTC](https://discourse.julialang.org/t/how-do-i-transform-a-vector-into-a-matrix/85032/5 "2022-07-31T09:45:02Z")

</div>

I’m fairly certain that your succinct version allocates a new matrix entirely, which the `reshape` version does not. May be relevant for someone doing that in a tight loop!

---

<div class="post-metadata">

**Author:** ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Post date:** [July 31, 2022, 8:13pm UTC](https://discourse.julialang.org/t/how-do-i-transform-a-vector-into-a-matrix/85032/6 "2022-07-31T20:13:11Z")

</div>

Very true! If you want to avoid copying, you can do:

```julia
julia> m = @view v[:,:]
3×1 view(::Matrix{Int64}, :, :) with eltype Int64:
 1
 2
 3

julia> m[2,1] *= 100
200

julia> v
3-element Vector{Int64}:
   1
 200
   3

```
