# Convert 1D Array to 1D Array of StaticArrays?

**URL:** <https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657>\
**Category:** General Usage\
**Tags:** arrays, convert, staticarrays\
**Created:** [September 3, 2021, 4:18pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657 "2021-09-03T16:18:46Z")\
**Posts on this page:** 12\
**Page:** 1

<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:** [September 3, 2021, 4:18pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/1 "2021-09-03T16:18:46Z")

</div>

Hello!

I think this should be fairly simple, suppose I have:

```julia
b = collect(1:999)

```

Where x,y, and z are given as:

```julia
x = b[1:3:end]
y = b[2:3:end]
z = b[3:3:end]

```

How would I then convert / parse this into a Vector of StaticArrays with 333 elements?

Is there a simple way to do it without for loops?

Kind regards

---

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [September 3, 2021, 4:27pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/2 "2021-09-03T16:27:01Z")

</div>

Are you looking for something like this?

```julia
[b[3*(i-1)+1:3i] for i = 1:333]

```

You can use StaticArrays if you want.  
I would suspect if this for loop can be avoided.

---

<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:** [September 3, 2021, 4:37pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/3 "2021-09-03T16:37:09Z")

</div>

> [@vavrines](#):
>
> `[b[3*(i-1)+1:3i] for i = 1:333]`

Yep, that does what I want and is a one liner. I did a small benchmark using BenchmarkTools:

```julia
@btime [b[3*(i-1)+1:3i] for i = 1:333]
  16.299 μs (669 allocations: 49.64 KiB)

```

And just for fun reshape

```julia
@btime reshape(b,(333,3))
  52.990 ns (2 allocations: 96 bytes)

```

So I had hoped that it would have been possible to “reshape/convert” b using another way to avoid the allocations. In this specific case it does not matter since both are fast, but for more real scenarios it will start to have an effect on performance for me.

Kind regards

---

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [September 3, 2021, 4:38pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/4 "2021-09-03T16:38:35Z")

</div>

You can try using `reinterpret` if you want to avoid allocations:

```julia
reinterpret(SVector{3, eltype(b)}, b)

```

---

<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:** [September 3, 2021, 4:50pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/5 "2021-09-03T16:50:21Z")

</div>

That worked too, giving this performance:

```julia
 @btime reinterpret(SVector{3, eltype(b)}, b)
  936.667 ns (10 allocations: 608 bytes)

```

So a lot better. Do you know though if the type of the reinterpret could become a “headache”? Now it is given as:

```julia
Base.ReinterpretArray{SArray{Tuple{3},Int64,1,3},1,Int64,Array{Int64,1}}

```

Seems like this is the best approach though, other than the return type or am I overthinking?

Kind regards

---

<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:** [September 3, 2021, 5:00pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/6 "2021-09-03T17:00:46Z")

</div>

Your benchmark results are skewed due to the fact that `b` is a non-const global variable (see [Manual · BenchmarkTools.jl](https://juliaci.github.io/BenchmarkTools.jl/dev/manual/#Interpolating-values-into-benchmark-expressions) ). If you fix that, you’ll get more realistic results:

```julia
julia> b = collect(1:999);

julia> f_comprehension(b) = [b[3*(i-1)+1:3i] for i = 1:333]
f_comprehension (generic function with 1 method)

julia> f_reshape(b) = reshape(b, (:, 3))
f_reshape (generic function with 1 method)

julia> using StaticArrays

julia> f_reinterpret(b) = reinterpret(SVector{3, eltype(b)}, b)
f_reinterpret (generic function with 1 method)

julia> using BenchmarkTools

julia> @btime f_comprehension($b);
  9.468 μs (334 allocations: 39.17 KiB)

julia> @btime f_reshape($b);
  42.198 ns (2 allocations: 96 bytes)

julia> @btime f_reinterpret($b);
  2.964 ns (0 allocations: 0 bytes)

```

The `reinterpret` approach is so fast as to be basically free (about 3000 times faster than the comprehension version), and it allocates no memory at all. If a vector of `SVector`s is what you want, then this is definitely the way to go.

As for the return type, it should be fine. You’ll need to make sure that the functions you pass that to expect an `AbstractVector` or an `AbstractArray`, but that’s good practice anyway.

---

<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:** [September 3, 2021, 5:05pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/7 "2021-09-03T17:05:35Z")

</div>

I see thanks, marked reinterpret as the answer 🙂

And thanks for the hints regarding using AbstractArrays etc., I did not think of that usually I would define numeric type too.

Kind regards

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 3, 2021, 7:33pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/8 "2021-09-03T19:33:53Z")

</div>

> [@Ahmed\_Salih](#):
>
> Seems like this is the best approach though, other than the return type or am I overthinking?

It is just ugly 🙂 But apparently there is no “solution” to the aesthetic problem.

> [@Simplify type information when reinterpreting/reshaping](https://discourse.julialang.org/t/simplify-type-information-when-reinterpreting-reshaping/66909):
>
> I frequently have to do things like this: julia\> x = rand(3,10) 3×10 Matrix{Float64}: 0.233999 0.217953 0.391807 0.396972 0.612708 0.344216 0.513826 0.724808 0.222591 0.668894 0.152236 0.0596347 0.297019 0.320482 0.864853 0.658225 0.72082 0.966251 0.429694 0.980746 0.181907 0.561663 0.991661 0.968927 0.800681 0.838888 0.624427 0.874867 0.818654 0.536921 julia\> y = reinterpret(reshape,SVector{3,Float64},x) 10-element reinterpret(reshape, SVector{3, Float64}, ::M…

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 12, 2021, 11:34pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/9 "2021-09-12T23:34:22Z")

</div>

I would like to add, because I just faced this, that for the reinterpretation of a 2D array to a vector of static vectors to actually work in function accepting AbstractVectors, one need to add a `vec` to the conversion:

```julia
julia> f(x::AbstractVector{<:AbstractVector}) = "this is a vector of vectors"
f (generic function with 1 method)

julia> x = rand(3,5)
3×5 Matrix{Float64}:
 0.33453 0.209665 0.0140571 0.385981 0.0500048
 0.922044 0.549503 0.826726 0.875412 0.200793
 0.418103 0.830735 0.913105 0.619941 0.740944

julia> y = vec(reinterpret(SVector{3,Float64},x)) # works in f
5-element reshape(reinterpret(SVector{3, Float64}, ::Matrix{Float64}), 5) with eltype SVector{3, Float64}:
 [0.3345302406715238, 0.9220439220026388, 0.4181031420691954]
 [0.2096650742720747, 0.5495028980965941, 0.8307349539720923]
 [0.014057112572854358, 0.8267260218514032, 0.9131049059956979]
 [0.3859812308760291, 0.8754122419901476, 0.6199408169713525]
 [0.050004821335951855, 0.2007929870776446, 0.7409443625689442]

julia> f(y)
"this is a vector of vectors"

julia> z = reinterpret(SVector{3,Float64},x) # does not work in f
1×5 reinterpret(SVector{3, Float64}, ::Matrix{Float64}):
 [0.33453, 0.922044, 0.418103] [0.209665, 0.549503, 0.830735] … [0.385981, 0.875412, 0.619941] [0.0500048, 0.200793, 0.740944]

julia> f(z)
ERROR: MethodError: no method matching f(::Base.ReinterpretArray{SVector{3, Float64}, 2, Float64, Matrix{Float64}, false})
Closest candidates are:
  f(::AbstractVector{var"#s2"} where var"#s2"<:(AbstractVector{T} where T)) at REPL[24]:1
Stacktrace:
 [1] top-level scope
   @ REPL[29]:1

```

**edit:** but noticed now that that is _not_ needed for the 1D array which is what the OP asked, because the array was already a vector and the reinterpreted array carries the type hierarchy of the original array (is that correct to say?)

---

<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:** [September 13, 2021, 4:59am UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/10 "2021-09-13T04:59:59Z")

</div>

What is the purpose of the ‘ugly’ type signatures for `reshape` and ‘reinterpret’? Is there any difference in layout between

> [@Ahmed\_Salih](#):
>
> `Base.ReinterpretArray{SArray{Tuple{3},Int64,1,3},1,Int64,Array{Int64,1}}`

and `Array{SVector{3, Int64},1}`?

I thought that the only thing that `reshape` and `reinterpret` did was to change the type tag on the underlying data. If so, why not simplify the type?

I seem to vaguely remember that is used to be different.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 13, 2021, 9:26am UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/11 "2021-09-13T09:26:32Z")

</div>

> [@lmiq](#):
>
> `y = vec(reinterpret(SVector{3,Float64},x))`

Is that better than:

```julia
x = rand(3,5)
y = SVector{5}(reinterpret(SVector{3,Float64},x))

```

?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 13, 2021, 12:10pm UTC](https://discourse.julialang.org/t/convert-1d-array-to-1d-array-of-staticarrays/67657/12 "2021-09-13T12:10:36Z")

</div>

In my MWE I didn’t mean to imply that `5` was typical (small) dimension. Usually I’m talking about particle simulations, and this dimension is on the tenths-of-thousands. So making that a static array is not possible.
