# How to "de-vectorize" a vector of string

**URL:** <https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211>\
**Category:** New to Julia\
**Created:** [April 14, 2017, 3:24am UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211 "2017-04-14T03:24:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [April 14, 2017, 3:24am UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/1 "2017-04-14T03:24:19Z")

</div>

Most likely it is something very easy I am missing but searched carefully the manual and couldn’t find a function to “de-vectorize” a vector of strings

Starting let’s say with

> `a = ["X", "Y", "Z"]`  
> `b = [a]`

now `b == [["X", "Y", "Z"]]`. I would like to find a function `f(.)` such that

> `b[f(a)] = a`

or `f(["X", "Y", "Z"]) = "X", "Y", "Z"`

Thank you for your help

---

<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:** [April 14, 2017, 4:13am UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/2 "2017-04-14T04:13:49Z")

</div>

I’m not entirely sure what you mean by

```julia
b[f(a)] = a

```

that would mean indexing into the vector `b` by the result of some function `f(a)`, which is possible but probably not what you want.

Can you provide a little more context about what you’re actually trying to accomplish?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 14, 2017, 4:15am UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/3 "2017-04-14T04:15:41Z")

</div>

> [@askvorts](#):
>
> b[f(a)] = a
> 
> or f([“X”, “Y”, “Z”]) = “X”, “Y”, “Z”

These are two different operations, which one do you want?

The second one is just splatting `f(a) = (a...)` should do it, though the function would not be type stable and it is not something you should do a lot if you care about performance.

`b[1]` will be equal to `a` so `f(a) = 1` works for the first one. Not sure how do you want it to depend on `a`…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 14, 2017, 4:24am UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/4 "2017-04-14T04:24:26Z")

</div>

Actually I guess you mean `[f(a)]` instead of `b[f(a)]`? That’ll match slightly better with the other version. If this is true, then no it’s impossible, `[f(a)]` will always return a single element array, no exceptions (unless you break the internal implementation since this behavior is implemented in julia after all), so you cannot make it equal to a multiple element array. `[a...]` would be the closest to what you are asking for but is strongly recomented against if you want to do this many times. `[a;]` is also equivalent in this case and should be much faster. Of course neither of them are useful since these are just fancy/inefficient ways to spell `copy(a)`. You need to be more specific about your actual problem.

---

<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:** [April 14, 2017, 5:23am UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/5 "2017-04-14T05:23:06Z")

</div>

> [@askvorts](#):
>
> b == [[“X”, “Y”, “Z”]]

Do you want things like

```julia
b = [["X", "Y", "Z"]]
first(b) #["X","Y","Z"]

```

or

```julia
(first(b)...) # "X","Y","Z"

```

Not sure what you’re asking

---

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [April 14, 2017, 2:31pm UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/6 "2017-04-14T14:31:32Z")

</div>

Thank you so much. The support from Julia community is amazing!  
I’ll try to be more precise by giving an example:

`ta` is a TimeArray where `ta.colnames` is string type  
` a = ["column10", "column50"]`  
`ta[a]`

Some results I got are:

> ` ta1 = ta[a]`` # MethodError`  
> `ta2 = ta[(a...)]``# MethodError`  
> `ta3 = ta[a...]``# OK`  
> `ta4 = ta[a;]``# MethodError`  
> `ta5 = ta[copy(a)]``# MethodError`

So `a...` at least works but, typical of me, it is strongly recommended against. Would this be the only option?

---

<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:** [April 14, 2017, 2:35pm UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/7 "2017-04-14T14:35:19Z")

</div>

```julia
new_ta = ta[first(a)]

```

?

---

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [April 14, 2017, 2:42pm UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/8 "2017-04-14T14:42:38Z")

</div>

> [@ChrisRackauckas](#):
>
> ```julia
> new_ta = ta[first(a)]
> 
> ```
> 
> ?

It almost works, but we only get the first `"column10"` of the two columns in `a`

---

<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:** [April 14, 2017, 2:46pm UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/9 "2017-04-14T14:46:02Z")

</div>

Oh, now you don’t have a Vector of Vectors?

I wouldn’t care about the splatting penalty here. It would give performance problems if you were doing really fast operations in a loop, but here you’re just pulling out a column. Splat and call it a day. Of you need something more, come back to this later and just loop.

---

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [April 14, 2017, 4:49pm UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/10 "2017-04-14T16:49:42Z")

</div>

Thank you both very much. Indeed this is the case. I’ll be splatting around just a few times. But although I can cope with the lack of elegance, the lack of performance is conceptually somewhat annoying.

---

<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:** [April 14, 2017, 5:55pm UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/11 "2017-04-14T17:55:36Z")

</div>

Could your vector `a` perhaps be replaced with a tuple instead? I’m not sure of all the details, but I think splatting with tuples is likely to be more efficient because their size is known at compile-time. Here’s a really simple example:

```julia
julia> f(x) = +(x...)
f (generic function with 1 method)

julia> using BenchmarkTools

julia> y1 = [1, 2]
2-element Array{Int64,1}:
 1
 2

julia> y2 = (1, 2)
(1,2)

julia> @benchmark f($y1)
BenchmarkTools.Trial:
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 61.206 ns (0.00% GC)
  median time: 66.964 ns (0.00% GC)
  mean time: 68.496 ns (0.00% GC)
  maximum time: 428.580 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 980
  time tolerance: 5.00%
  memory tolerance: 1.00%

julia> @benchmark f($y2)
BenchmarkTools.Trial:
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 1.850 ns (0.00% GC)
  median time: 1.856 ns (0.00% GC)
  mean time: 1.875 ns (0.00% GC)
  maximum time: 14.586 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000
  time tolerance: 5.00%
  memory tolerance: 1.00%

```

---

<div class="post-metadata">

**Author:** ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)\
**Post date:** [April 14, 2017, 8:10pm UTC](https://discourse.julialang.org/t/how-to-de-vectorize-a-vector-of-string/3211/12 "2017-04-14T20:10:39Z")

</div>

@rdeits thank you very much. It worked like a charm. Also got a \>30x speed improvement! Julia is indeed a very rewarding language.
