# Matrices with vectors in elements?

**URL:** <https://discourse.julialang.org/t/matrices-with-vectors-in-elements/37949>\
**Category:** General Usage\
**Created:** [April 21, 2020, 7:37am UTC](https://discourse.julialang.org/t/matrices-with-vectors-in-elements/37949 "2020-04-21T07:37:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 21, 2020, 7:37am UTC](https://discourse.julialang.org/t/matrices-with-vectors-in-elements/37949/1 "2020-04-21T07:37:04Z")

</div>

I’m a little puzzled about how to fill vectors into matrix elements… Consider the two attempts below: the first is unsuccessful, the second is successful.

```julia
julia> # Unsuccessful strategy
julia> a11 = [1,2];
julia> a12 = [1,-2];
julia> a21 = [1,2,1];
julia> a22 = [1,2,1];

julia> Matrix{Vector}([a11 a12; a21 a22])
ERROR: MethodError: Cannot `convert` an object of type
  Int64 to an object of type
  Array{T,1} where T
Closest candidates are:
  convert(::Type{T}, ::AbstractArray) where T<:Array at array.jl:533
  convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:14
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at D:\buildbot\worker\package_win64\build\usr\sh
are\julia\stdlib\v1.4\LinearAlgebra\src\factorization.jl:55
  ...
Stacktrace:
 [1] setindex!(::Array{Array{T,1} where T,2}, ::Int64, ::Int64) at .\array.jl:825
 [2] copyto!(::Array{Array{T,1} where T,2}, ::Array{Int64,2}) at .\multidimensional.jl:962
 [3] Array{Array{T,1} where T,2}(::Array{Int64,2}) at .\array.jl:541
 [4] top-level scope at REPL[34]:1

julia> # Successful strategy
julia> A = Matrix{Vector}(undef,2,2)
2×2 Array{Array{T,1} where T,2}:
 #undef #undef
 #undef #undef

julia> A[1,1] = a11;
julia> A[1,2] = a12;
julia> A[2,1] = a21;
julia> A[2,2] = a22;

julia> A
2×2 Array{Array{T,1} where T,2}:
 [1, 2] [1, -2]
 [1, 2, 1] [1, 2, 1]

```

Question: is there a way to fill the matrix with vector elements using the first notation? [The first notation is more convenient as an argument in function calls.]

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 21, 2020, 7:54am UTC](https://discourse.julialang.org/t/matrices-with-vectors-in-elements/37949/2 "2020-04-21T07:54:06Z")

</div>

The `[` here is lowered as `hvcat`. MWE for the issue you are experiencing:

```julia
julia> a = 1:2
1:2

julia> [a a; a a]
4×2 Array{Int64,2}:
 1 1
 2 2
 1 1
 2 2

```

I usually work around these with `reshape`, eg use the equivalent of

```julia
julia> reshape([a, a, a, a], 2, :)
2×2 Array{UnitRange{Int64},2}:
 1:2 1:2
 1:2 1:2

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 21, 2020, 8:02am UTC](https://discourse.julialang.org/t/matrices-with-vectors-in-elements/37949/3 "2020-04-21T08:02:30Z")

</div>

Neat. I notice I have to list the matrix elements _column wise_, which makes sense.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [April 21, 2020, 11:36am UTC](https://discourse.julialang.org/t/matrices-with-vectors-in-elements/37949/4 "2020-04-21T11:36:16Z")

</div>

You can also wrap each vector in another vector to avoid concatenation:

```julia
julia> [[a11] [a12]; [a21] [a22]]
2×2 Array{Array{Int64,1},2}:
 [1, 2] [1, -2]
 [1, 2, 1] [1, 2, 1]

```

Also note that `Matrix{Vector}` creates a matrix with non-concretely typed elements and accessing elements will not be type stable. You will probably want to specify the element type of the vectors explicitly, like `Matrix{Vector{Int}}`.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [April 21, 2020, 3:33pm UTC](https://discourse.julialang.org/t/matrices-with-vectors-in-elements/37949/5 "2020-04-21T15:33:46Z")

</div>

Another option:

```julia
julia> [[a11, a21] [a12, a22]]
2×2 Array{Array{Int64,1},2}:
 [1, 2] [1, -2]
 [1, 2, 1] [1, 2, 1]

```
