# Efficient conversion from StaticArray to Base Array?

**URL:** <https://discourse.julialang.org/t/efficient-conversion-from-staticarray-to-base-array/45825>\
**Category:** New to Julia\
**Created:** [August 31, 2020, 12:42pm UTC](https://discourse.julialang.org/t/efficient-conversion-from-staticarray-to-base-array/45825 "2020-08-31T12:42:25Z")\
**Posts on this page:** 7\
**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:** [August 31, 2020, 12:42pm UTC](https://discourse.julialang.org/t/efficient-conversion-from-staticarray-to-base-array/45825/1 "2020-08-31T12:42:25Z")

</div>

Hello!

Suppose I have an array defined as such:

```julia
using StaticArrays
b = Array{StaticArrays.SArray{Tuple{3},Float32,1,3},1}([[1,1,1],[2,2,2],[3,3,3]])

```

And I want it to be transformed to a convential:

```julia
 Array{Array{Float32,3},1}

```

I have not found an efficient way to do so, and my approach using iterators gives me a very high amount of allocations…

The reason I want to do this in the first place is that I am interfacing with a Julia library which interfaces with something else, and it needs to be fed standard arrays.

Kind regards

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 31, 2020, 1:13pm UTC](https://discourse.julialang.org/t/efficient-conversion-from-staticarray-to-base-array/45825/2 "2020-08-31T13:13:45Z")

</div>

What do you mean by a high amount of allocations. If you have a `Vector` of `SArray` and wants to create from it a `Vector` of `Array`, you will need to make an allocation for each new `Array` (i.e., for each element of the outer vector).

`SArray` is a static array, this means it is possible that a `Vector` of `SArray`s is stored contiguously inside the `Vector` and just one allocation is done. For a `Vector` or `Array` you almost surely will have each element as a “pointer” to a heap object allocated separately. Again, `SArray` is immutable, `Arrays` is mutable, do you need the mutability? If you need it maybe the best solution is to have a single 4-dimensional `Array`, instead of a `Vector` of 3-dimensional `SArray`s.

---

<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:** [August 31, 2020, 1:25pm UTC](https://discourse.julialang.org/t/efficient-conversion-from-staticarray-to-base-array/45825/3 "2020-08-31T13:25:00Z")

</div>

Sorry, I might not have explained it properly. I ended up fixing my mistake:

```julia
using StaticArrays

x = zeros(100)

r = [rand(3,1) for i in x]

b = Array{StaticArrays.SArray{Tuple{3},Float32,1,3},1}(r)

@time b_flat = collect(Iterators.flatten(Iterators.flatten(b)))

nr = size(b)[1]
nc = size(b[1])[1]

# I had changed place of nr and nc, so that gave much higher allocations
@time b_arr = reshape(b_arr,(nr,nc))

```

This code gives very few allocations and does what I want. If one can improve it I would like to learn

Kind regards

---

<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:** [August 31, 2020, 1:28pm UTC](https://discourse.julialang.org/t/efficient-conversion-from-staticarray-to-base-array/45825/4 "2020-08-31T13:28:10Z")

</div>

I do not need mutability as such, I just need in the shape of a typical Julia array to work properly with the external package.

Kind regards

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [August 31, 2020, 1:34pm UTC](https://discourse.julialang.org/t/efficient-conversion-from-staticarray-to-base-array/45825/5 "2020-08-31T13:34:02Z")

</div>

`reinterpret(Float32, b')` might work, give it a whirl.

---

<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:** [August 31, 2020, 1:41pm UTC](https://discourse.julialang.org/t/efficient-conversion-from-staticarray-to-base-array/45825/6 "2020-08-31T13:41:53Z")

</div>

> [@tim.holy](#):
>
> `reinterpret(Float32, b')`

Thanks that indeed worked! Any suggestions of how to get the type, other than doing:

```julia
typeof(b[1][1])
Float32

```

Kind regards

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [August 31, 2020, 3:25pm UTC](https://discourse.julialang.org/t/efficient-conversion-from-staticarray-to-base-array/45825/7 "2020-08-31T15:25:41Z")

</div>

`eltype(eltype(b))`
