# How to efficiently create a vector from a matrix comprehensions?

**URL:** <https://discourse.julialang.org/t/how-to-efficiently-create-a-vector-from-a-matrix-comprehensions/146>\
**Category:** General Usage\
**Created:** [November 6, 2016, 7:27pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-vector-from-a-matrix-comprehensions/146 "2016-11-06T19:27:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [November 6, 2016, 7:27pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-vector-from-a-matrix-comprehensions/146/1 "2016-11-06T19:27:03Z")

</div>

Since v0.5 comprehensions are shape-preserving ([0.5-style comprehensions by JeffBezanson · Pull Request #16622 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/16622)), so

```julia
m = [x for x in [1 2; 3 4]]

```

results in a matrix.

How should one, using a comprehension, create a vector instead? Something that better reflects a traditional `for` loop:

```julia
v = Int[]
for x in [1 2; 3 4]
    push!(v, x)
end

```

Preferably in an efficient way, **with no temporaries**.

---

<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 6, 2016, 8:46pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-vector-from-a-matrix-comprehensions/146/2 "2016-11-06T20:46:01Z")

</div>

> [@cdsousa](#):
>
> m = [x for x in [1 2; 3 4]]

`m = [x for x in vec([1 2; 3 4])]`? `vec` just uses a view and does’t copy.

---

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [November 6, 2016, 8:49pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-vector-from-a-matrix-comprehensions/146/3 "2016-11-06T20:49:30Z")

</div>

> [@ChrisRackauckas](#):
>
> vec just uses a view and does’t copy.

It doesn’t seem so:

```julia
julia> typeof(vec([1 2; 3 4]))
Array{Int64,1}

```

---

<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 6, 2016, 8:56pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-vector-from-a-matrix-comprehensions/146/4 "2016-11-06T20:56:29Z")

</div>

> [@cdsousa](#):
>
> julia\> typeof(vec([1 2; 3 4]))  
> Array{Int64,1}

That’s not testing anything. Arrays are views too.

```julia
julia> const v = [1 2;3 4]
2×2 Array{Int64,2}:
 1 2
 3 4

julia> @time vec(v)
  0.000001 seconds (6 allocations: 240 bytes)
4-element Array{Int64,1}:
 1
 3
 2
 4

julia> const w = [1 2 3 4 5 6;3 4 7 7 8 9]
2×6 Array{Int64,2}:
 1 2 3 4 5 6
 3 4 7 7 8 9

julia> @time vec(w)
  0.000002 seconds (6 allocations: 240 bytes)
12-element Array{Int64,1}:
 1
 3
 2
 4
 3
 7
 4
 7
 5
 8
 6
 9

julia> z = vec(w)
12-element Array{Int64,1}:
 1
 3
 2
 4
 3
 7
 4
 7
 5
 8
 6
 9

julia> z[1] = 10
10

julia> w
2×6 Array{Int64,2}:
 10 2 3 4 5 6
  3 4 7 7 8 9

```

No allocations, just the allocation of the view which is constant 240 bytes. The array just tells it how to index. Once Tim Holy explained that, the magic went away.

> <https://stackoverflow.com/questions/38777469/function-for-reshape-view>

---

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [November 6, 2016, 9:08pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-vector-from-a-matrix-comprehensions/146/5 "2016-11-06T21:08:22Z")

</div>

Wow! That is something completely new to me! And, at a first sight, this kind of hidden aliasing seems a little bit too dangerous…

Thanks @ChrisRackauckas.

By the way, besides that comment by @tim.holy, is there any documentation on this?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 7, 2016, 12:25am UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-vector-from-a-matrix-comprehensions/146/6 "2016-11-07T00:25:36Z")

</div>

`vec` really doesn’t copy:

```julia
julia> A = [1 2; 3 4]
2×2 Array{Int64,2}:
 1 2
 3 4

julia> v = vec(A)
4-element Array{Int64,1}:
 1
 3
 2
 4

julia> A[1,1] = 11
11

julia> v
4-element Array{Int64,1}:
 11
  3
  2
  4

```
