# Create a vector of SVectors

**URL:** <https://discourse.julialang.org/t/create-a-vector-of-svectors/61393>\
**Category:** New to Julia\
**Tags:** staticarrays\
**Created:** [May 18, 2021, 8:53pm UTC](https://discourse.julialang.org/t/create-a-vector-of-svectors/61393 "2021-05-18T20:53:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptnz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptnz/32/24519_2.png) [@baptnz](https://discourse.julialang.org/u/baptnz)\
**Post date:** [May 18, 2021, 8:53pm UTC](https://discourse.julialang.org/t/create-a-vector-of-svectors/61393/1 "2021-05-18T20:53:08Z")

</div>

I have 3 independent vectors `x,y,z` of length N, and with to turn them into N 3-SVectors. I wonder if there’s a better way to do this:

```julia
    using StaticArrays
    x = rand(10) # dummy example
    y = rand(10) # dummy example
    z = rand(10) # dummy example

    positions = map((x,y,z) -> SVector(x,y,z), x, y, z) # better way to create this?

```

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [May 18, 2021, 9:01pm UTC](https://discourse.julialang.org/t/create-a-vector-of-svectors/61393/2 "2021-05-18T21:01:18Z")

</div>

That’s good. Could also do `map(SVector, x, y, z)` or `SVector.(x, y, z)`.

---

<div class="post-metadata">

**Author:** ![baptnz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptnz/32/24519_2.png) [@baptnz](https://discourse.julialang.org/u/baptnz)\
**Post date:** [May 18, 2021, 9:11pm UTC](https://discourse.julialang.org/t/create-a-vector-of-svectors/61393/3 "2021-05-18T21:11:10Z")

</div>

Oh yes, of course! I tried more complicated things with broadcast, but this was right there. Thanks. Out of curiosity, is there some penalty in mapping with an anonymous function, i.e.

```julia
map((x,y,z) -> SVector(x,y,z), x, y, z)

```

vs

```julia
SVector.(x,y,z)

```

(not worried about this case, but more to know the general pattern isn’t a bad habit – extraneous anonymous function calls would have some penalty in R, for example)

---

<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:** [May 18, 2021, 9:16pm UTC](https://discourse.julialang.org/t/create-a-vector-of-svectors/61393/4 "2021-05-18T21:16:54Z")

</div>

Nope, it’s all good. Calling `SVector.(x, y, z)` actually just does (roughly):

```julia
broadcast((x, y, z) -> SVector(x, y, z), x, y, z)

```

creating an anonymous function and calling it on each element of `x`, `y`, and `z`. Anonymous functions in Julia are fast (just as fast as any other function), and they get optimized an inlined just as well.

---

<div class="post-metadata">

**Author:** ![baptnz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptnz/32/24519_2.png) [@baptnz](https://discourse.julialang.org/u/baptnz)\
**Post date:** [May 18, 2021, 9:24pm UTC](https://discourse.julialang.org/t/create-a-vector-of-svectors/61393/5 "2021-05-18T21:24:33Z")

</div>

Thanks – that was my concern, not knowing if they might be inlined by the compiler.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 18, 2021, 9:28pm UTC](https://discourse.julialang.org/t/create-a-vector-of-svectors/61393/6 "2021-05-18T21:28:08Z")

</div>

In general, Julia is very aggressive in inlining. There are some cases where it still messes up (in both directions), but for the most part the inliner is pretty good. (The biggest exception I know of is LLVM calls).
