# How to force single column array?

**URL:** <https://discourse.julialang.org/t/how-to-force-single-column-array/26746>\
**Category:** General Usage\
**Created:** [July 24, 2019, 2:39pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746 "2019-07-24T14:39:43Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [July 25, 2019, 10:22am UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/21 "2019-07-25T10:22:25Z")

</div>

Thanks! Will try it out and read a bit of documentation and will come back again if I still don’t get it or got it to work as I want.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [July 25, 2019, 11:44am UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/22 "2019-07-25T11:44:37Z")

</div>

Got it, it works now! Thanks for your time and same to everybody else who helped me out 🙂

My final question is whether there is an easy way to “unpack” the data? Since now when I run the command for let us say positions I get:

```julia
a[1]
125751-element Array{SArray{Tuple{3},Float32,1,3},1}:
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.02]
 [0.0, 0.0, 0.04]
 [0.02, 0.0, 0.0]
 [0.04, 0.0, 0.0]
 [0.06, 0.0, 0.0]
 [0.08, 0.0, 0.0]
 [0.1, 0.0, 0.0]
 [0.12, 0.0, 0.0]
 [0.14, 0.0, 0.0]
 [0.16, 0.0, 0.0]
 [0.18, 0.0, 0.0]
 [0.2, 0.0, 0.0]

```

Which is great, since now every particle has a vector assigned with its relevant positions as we talked about - in my former way of doing it though, I could easily make a scatter plot of the positions using:

```julia
scatter(a[1][:,1],a[1][:,3])

```

Since it was in matrix form and not vector form - is there a simple way to “unpack” all the vectors so they become a “Array{Float32,2}” again? I can do it easily with a for loop, I just want to make sure I don’t miss anything basic.Just to provide an example for what I mean with unpacked data which made it easy to use scatter command:

```julia
2×3 Array{Float32,2}:
 0.0 0.0 0.0
 0.0 0.0 0.2
 0.0 0.0 0.04
...

```

And as you said I saw a small performance increase and I am sure it is going to be magnified when I start doing calculations on the vectors down the line 🙂

Kind regards

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [July 25, 2019, 12:01pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/23 "2019-07-25T12:01:23Z")

</div>

You could probably use [RecursiveArrayTools.jl](https://github.com/JuliaDiffEq/RecursiveArrayTools.jl) for this. Try `v = VectorOfArray(a[1])` which should then be indexable like a matrix.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 25, 2019, 12:01pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/24 "2019-07-25T12:01:55Z")

</div>

Yes, you can use `reinterpret(Float32, x)` to interpret a vector of SVectors as a matrix.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 25, 2019, 12:04pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/25 "2019-07-25T12:04:36Z")

</div>

Err, sorry, I think that will actually give you a flat vector, not a matrix. I’m not at a computer right now, but I think you want `reshape(reinterpret(Float32, x), 3, :)`

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [July 25, 2019, 12:44pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/26 "2019-07-25T12:44:05Z")

</div>

Thanks your solution works for my purpose.

I might try looking at @carstenbauer if I need to do it more often in the future.

Thanks for all the help in this thread.

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 25, 2019, 1:03pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/27 "2019-07-25T13:03:52Z")

</div>

The `reinterpret` approach is the best if you are going to be doing anything else with the data but if you just want to do things like

> [@Ahmed\_Salih](#):
>
> scatter(a[1][:,1],a[1][:,3])

then a broadcasted `getindex` works well

```julia
scatter(getindex.(a[1], 1), getindex.(a[1], 3))

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [July 25, 2019, 1:14pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/28 "2019-07-25T13:14:27Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![wiktorkujawa](https://avatars.discourse-cdn.com/v4/letter/w/bc8723/32.png) [@wiktorkujawa](https://discourse.julialang.org/u/wiktorkujawa)\
**Post date:** [July 25, 2019, 7:50pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/29 "2019-07-25T19:50:38Z")

</div>

Here’s also my way:  
`a=Array{Int32}(collect(zeros(4)))`  
or

```julia
b=4
a=Array(collect(zeros(b)))

```

or maybe:

```julia
b=rand(5,6)
a=Array(vcat(b...))

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 25, 2019, 8:40pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/30 "2019-07-25T20:40:26Z")

</div>

> [@wiktorkujawa](#):
>
> ```julia
> a=Array{Int32}(collect(zeros(4)))
> 
> ```

Just write

```julia
a = zeros(Int32, 4)

```

No need to `collect` `zeros`, it’s already collected. And no point in wrapping it in `Array`, it’s already an array.

> [@wiktorkujawa](#):
>
> ```julia
> b=rand(5,6) 
> a=Array(vcat(b...))
> 
> ```

This is just

```julia
a = vec(b)

```

Don’t use splatting for something like this, it is _super slow_:

```julia
julia> @btime vec($b);
  35.559 ns (2 allocations: 80 bytes)

julia> @btime Array(vcat($b...));
  902.184 ns (32 allocations: 1.13 KiB)

```

---

<div class="post-metadata">

**Author:** ![wiktorkujawa](https://avatars.discourse-cdn.com/v4/letter/w/bc8723/32.png) [@wiktorkujawa](https://discourse.julialang.org/u/wiktorkujawa)\
**Post date:** [July 25, 2019, 8:50pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/31 "2019-07-25T20:50:21Z")

</div>

That’s probably due to my tuple into array convert practice, but in this example it works like you said.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 25, 2019, 9:28pm UTC](https://discourse.julialang.org/t/how-to-force-single-column-array/26746/32 "2019-07-25T21:28:28Z")

</div>

If you really need to turn an iterable into a vector (but you should very rarely do that) then try to use `collect` first. If it’s a tuple, `collect` will turn it into a vector. There’s no need to splat and vcat and then wrap it in an `Array` call on top of everything else. Just a simple `collect(b)` will do.

If your code looks complicated, then you are probably overcomplicating it 😉

[Previous page](https://discourse.julialang.org/t/how-to-force-single-column-array/26746.md?page=1)
