# Create a row matrix of vectors

**URL:** <https://discourse.julialang.org/t/create-a-row-matrix-of-vectors/165>\
**Category:** General Usage\
**Created:** [November 7, 2016, 2:40pm UTC](https://discourse.julialang.org/t/create-a-row-matrix-of-vectors/165 "2016-11-07T14:40:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [November 7, 2016, 2:40pm UTC](https://discourse.julialang.org/t/create-a-row-matrix-of-vectors/165/1 "2016-11-07T14:40:19Z")

</div>

I’d like to be able to create a row matrix holding vectors (the purpose is to pass them as series to Plots, but the question is general). However, I get issues with concatenation. I can create a vector of different-length vectors easily: ` a = [rand(n) for n in 2:5]` but `a'` fails with an error relating to concatenating different length vectors. It does seem possible to create a 2-d array of arrays:

```julia
test = Matrix{Any}(5,5)
fill!(test, rand(5))

```

works, but only as long as the arrays all have the same length. Am I abusing the syntax?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [November 7, 2016, 3:15pm UTC](https://discourse.julialang.org/t/create-a-row-matrix-of-vectors/165/2 "2016-11-07T15:15:36Z")

</div>

Silly me, I can just use `reshape`. Sorry

---

<div class="post-metadata">

**Author:** ![Balinus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/balinus/32/243_2.png) [@Balinus](https://discourse.julialang.org/u/Balinus)\
**Post date:** [November 11, 2016, 2:23am UTC](https://discourse.julialang.org/t/create-a-row-matrix-of-vectors/165/3 "2016-11-11T02:23:29Z")

</div>

Could you provide an example of what you wanted and your solution? This might be useful for me and others.

Thanks!

---

<div class="post-metadata">

**Author:** ![JoshLangsfeld](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshlangsfeld/32/8211_2.png) [@JoshLangsfeld](https://discourse.julialang.org/u/JoshLangsfeld)\
**Post date:** [November 12, 2016, 10:44pm UTC](https://discourse.julialang.org/t/create-a-row-matrix-of-vectors/165/4 "2016-11-12T22:44:36Z")

</div>

You can skip the reshape (or transpose) call by using multi-dimensional [comprehension syntax](http://docs.julialang.org/en/release-0.5/manual/arrays/#comprehensions):

```julia
julia> [rand(n) for i=1:1, n=1:3]
1×3 Array{Array{Float64,1},2}:
  [0.19346] [0.875024,0.573032] [0.26811,0.288699,0.481266]

```

Note that just using `i=1` still only produces a 1-D vector since as a scalar it’s making a contribution of 0 to the overall result dimension.

Also, note that using `fill!` is probably wrong here because it’s only allocating and building a single vector of numbers and putting a reference to that same vector object in each place in the matrix.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 16, 2016, 2:40am UTC](https://discourse.julialang.org/t/create-a-row-matrix-of-vectors/165/5 "2016-11-16T02:40:47Z")

</div>

```julia
reshape(v,1,length(v))

```

should `reshape` a column vector to a row vector. Since `reshape` is a view it should be pretty cost-free too.

One place this comes up is when working with Plots.jl. A series is usually a column, and so rows denote the different series. So giving something like a labels for legends needs to be a row vector. I used to just transpose the vector of names (strings) I’d get in my recipes, but that’s being deprecated. The `reshape` trick works just fine though.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [November 21, 2016, 7:56am UTC](https://discourse.julialang.org/t/create-a-row-matrix-of-vectors/165/6 "2016-11-21T07:56:51Z")

</div>

Thanks, Chris, was late to respond.
