# How do I create vectors from matrix columns?

**URL:** <https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139>\
**Category:** General Usage\
**Created:** [June 7, 2017, 2:39pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139 "2017-06-07T14:39:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![phillefjonk](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@phillefjonk](https://discourse.julialang.org/u/phillefjonk)\
**Post date:** [June 7, 2017, 2:39pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/1 "2017-06-07T14:39:49Z")

</div>

What is the most simple syntax for extracting a matrix columns into separate vectors?  
My matrix is in a text file (with the name data.dat) with 19 columns and 30000 rows , where each row ends with a line feed \n

In principle, say I have this matrix in the text file:

1 2 3 \n  
4 5 6 \n  
7 8 9 \n

And want the vectors  
v1 = [1, 4, 7]  
v2 = [2, 5, 8]  
v3 = [3, 6, 9]

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 7, 2017, 2:58pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/2 "2017-06-07T14:58:26Z")

</div>

There might be a neat trick that is simpler, but what about

```julia
julia> a = [1 2 3; 4 5 6; 7 8 9];

julia> (v1, v2, v3) = [a[:,x] for x in 1:size(a,1)]
3-element Array{Array{Int64,1},1}:
 [1, 4, 7]
 [2, 5, 8]
 [3, 6, 9]

```

after creating the matrix (probably using `readdlm`)?

---

<div class="post-metadata">

**Author:** ![phillefjonk](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@phillefjonk](https://discourse.julialang.org/u/phillefjonk)\
**Post date:** [June 7, 2017, 4:30pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/3 "2017-06-07T16:30:52Z")

</div>

Thanks,

I solved it by:

```julia
file = open("data.dat")
data = readdlm("data.dat")
close(file)

v1 = data[:,1] 
v1 = transpose(v1)

v2 = data[:,2] 
v2 = transpose(v2)

# ... and so forth for all columns

```

---

<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:** [June 7, 2017, 5:19pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/4 "2017-06-07T17:19:03Z")

</div>

You don’t need the `transpose`. Just `data[:,1]` works fine. Also, you don’t need to `open` or `close` the file. @anon94023334 's approach works great.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 7, 2017, 5:28pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/5 "2017-06-07T17:28:53Z")

</div>

It’s probable that you want views instead of copies:

```julia
columns = [view(a, :, i) for i in 1:size(a, 2)]

```

Also note that you want `size(a, 2)`, rather than `size(a, 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:** [June 7, 2017, 7:34pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/6 "2017-06-07T19:34:10Z")

</div>

I’ve always thought it’d be nice with a method that allowed you to specify `size(a, :columns)` (or `mean`, `sum`, etc.)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 7, 2017, 8:31pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/7 "2017-06-07T20:31:54Z")

</div>

I have several times wanted a columns(M) function that returned that vector of views.

---

<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:** [June 7, 2017, 8:41pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/8 "2017-06-07T20:41:21Z")

</div>

Or even better, that returned an iterator across the columns which could be collected to form that Vector. That’d actually be fairly easy to implement, I guess 🙂 But maybe not much is gained by an iterator if it’s already views?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 7, 2017, 8:42pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/9 "2017-06-07T20:42:55Z")

</div>

```julia
julia> columns(M) = (view(M, :, i) for i in 1:size(M, 2))
columns (generic function with 1 method)

julia> M = rand(3, 3)
3×3 Array{Float64,2}:
 0.594998 0.565428 0.19341
 0.522016 0.651766 0.39392
 0.652366 0.59112 0.375274

julia> columns(M)
Base.Generator{UnitRange{Int64},##3#4{Array{Float64,2}}}(#3, 1:3)

julia> for i in columns(M)
       println(i)
       end
[0.594998, 0.522016, 0.652366]
[0.565428, 0.651766, 0.59112]
[0.19341, 0.39392, 0.375274]

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 7, 2017, 8:44pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/10 "2017-06-07T20:44:09Z")

</div>

The problem with an iterator is that you can’t do `columns(M)[2]`.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 7, 2017, 8:45pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/11 "2017-06-07T20:45:43Z")

</div>

But maybe it’s all a bit pointless, since these days you can write

```julia
ulia> @view M[:, 2]
3-element SubArray{Float64,1,Array{Float64,2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true}:
 0.565428
 0.651766
 0.59112

```

---

<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:** [June 7, 2017, 8:58pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/12 "2017-06-07T20:58:08Z")

</div>

I don’t think it’s pointless - I like `for col in columns(M)` 🙂

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 7, 2017, 10:38pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/13 "2017-06-07T22:38:09Z")

</div>

Yes, me too!

---

<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:** [June 7, 2017, 10:57pm UTC](https://discourse.julialang.org/t/how-do-i-create-vectors-from-matrix-columns/4139/14 "2017-06-07T22:57:47Z")

</div>

I think it could also solve the issue of colwise inplace operations, e.g. [https://github.com/JuliaData/DataTables.jl/issues/36](https://github.com/JuliaData/DataTables.jl/issues/36) EDIT No that’s silly - because DataFrames have `eachcol` 😊
