# How to unvectorize a vector to use it as an argument of a function

**URL:** <https://discourse.julialang.org/t/how-to-unvectorize-a-vector-to-use-it-as-an-argument-of-a-function/122441>\
**Category:** New to Julia\
**Created:** [November 9, 2024, 2:58am UTC](https://discourse.julialang.org/t/how-to-unvectorize-a-vector-to-use-it-as-an-argument-of-a-function/122441 "2024-11-09T02:58:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wooheon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wooheon/32/206569_2.png) [@Wooheon](https://discourse.julialang.org/u/Wooheon)\
**Post date:** [November 9, 2024, 2:58am UTC](https://discourse.julialang.org/t/how-to-unvectorize-a-vector-to-use-it-as-an-argument-of-a-function/122441/1 "2024-11-09T02:58:54Z")

</div>

Hello.

I am trying to use a vector output of some function as an input of another function.  
For example, if `ϕₛₑᵣᵢₑₛ₄` is a function that has 17 inputs and `coef` is a vector of 15 elements, I want to write down `ϕₛₑᵣᵢₑₛ₄(coef,input_16,input_17)`.  
But when I do this I get an error that shows that the last 14 inputs are not being used, which means `coef` is just passed as a single argument.  
I think this would be very elementary question, but can someone let me know how to put `coef` as an input easily than just writing down `(coef[1],coef[2],...,coef[15])`?  
Please let me know.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [November 9, 2024, 3:23am UTC](https://discourse.julialang.org/t/how-to-unvectorize-a-vector-to-use-it-as-an-argument-of-a-function/122441/2 "2024-11-09T03:23:15Z")

</div>

This is what “splatting” is for. This should work:

```julia
ϕₛₑᵣᵢₑₛ₄(coef..., input_16,input_17)

```

---

<div class="post-metadata">

**Author:** ![Wooheon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wooheon/32/206569_2.png) [@Wooheon](https://discourse.julialang.org/u/Wooheon)\
**Post date:** [November 9, 2024, 3:36am UTC](https://discourse.julialang.org/t/how-to-unvectorize-a-vector-to-use-it-as-an-argument-of-a-function/122441/3 "2024-11-09T03:36:35Z")

</div>

It worked! Thanks a lot!!

Best,  
Wooheon.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [November 11, 2024, 4:18pm UTC](https://discourse.julialang.org/t/how-to-unvectorize-a-vector-to-use-it-as-an-argument-of-a-function/122441/4 "2024-11-11T16:18:03Z")

</div>

As a remark, splatting a `Vector` almost always results in suboptimal performance (due to dynamic dispatch). You might instead consider indexing `coef` _inside_ the function rather than splatting outside. I.e., rather than

```julia
function ϕ(coef1, coef2, coef3, coef4, input1, input2)
  # do stuff
end

```

do something like

```julia
function ϕ(coefs, input1, input2)
  coef1 = coefs[1] # destructure by indexing
  coef2 = coefs[2]
  coef3 = coefs[3]
  coef4 = coefs[4]
  # do stuff
end

```

or

```julia
function ϕ(coefs, input1, input2)
  coef1, coef2, coef3, coef4 = coefs # destructure by iteration
  # do stuff
end

```

This isn’t too different to define as a function than one that takes each `coef` as its own input. But this avoids the performance pitfalls of splatting.

Alternatively, if you’re going to splat, consider using a `Tuple` instead of `Vector` and splatting that instead. Since a `Tuple`’s type defines its size, they tend to splat more performantly at small sizes.

---

<div class="post-metadata">

**Author:** ![Wooheon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wooheon/32/206569_2.png) [@Wooheon](https://discourse.julialang.org/u/Wooheon)\
**Post date:** [November 12, 2024, 2:05pm UTC](https://discourse.julialang.org/t/how-to-unvectorize-a-vector-to-use-it-as-an-argument-of-a-function/122441/5 "2024-11-12T14:05:04Z")

</div>

Thanks a lot for the instructive examples and explanation. I appreciate it a lot!

Best,  
Wooheon
