# How to broadcast a Vector of an Array

**URL:** <https://discourse.julialang.org/t/how-to-broadcast-a-vector-of-an-array/11418>\
**Category:** General Usage\
**Created:** [June 4, 2018, 4:14pm UTC](https://discourse.julialang.org/t/how-to-broadcast-a-vector-of-an-array/11418 "2018-06-04T16:14:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shekhar7gpt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shekhar7gpt/32/4348_2.png) [@shekhar7gpt](https://discourse.julialang.org/u/shekhar7gpt)\
**Post date:** [June 4, 2018, 4:14pm UTC](https://discourse.julialang.org/t/how-to-broadcast-a-vector-of-an-array/11418/1 "2018-06-04T16:14:14Z")

</div>

I try to convert a 2-dimensional array of floats to a 1-dimensional array of Quaternions. I want to use broadcasting for this. The following code fails. How can it be fixed?

```julia
using Rotations, StaticArrays

function Quat(vec::SVector{4,Float64})
    return Quat(vec[1], vec[2], vec[3], vec[4])
end

q1 = [0.287819, -0.0776879, 0.208938, 0.931381]

q2 = [0.287819, -0.0776879, 0.208938, 0.931381]

quat = [q1 q2]'

new_quat = Quat.(quat)

```

The error message is:

```julia
ERROR: LoadError: MethodError: no method matching Quat(::Float64)
Closest candidates are:
  Quat(::StaticArrays.SArray{Tuple{4},Float64,1,4}) at /home/shekhar/uavtalk/scripts/test_quat.jl:5
Stacktrace:
 [1] broadcast_t(::Function, ::Type{Any}, ::Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}, ::CartesianRange{CartesianIndex{2}}, ::Array{Float64,2}) at ./broadcast.jl:258
 [2] broadcast_c at ./broadcast.jl:321 [inlined]
 [3] broadcast(::Function, ::Array{Float64,2}) at ./broadcast.jl:455
 [4] include_from_node1(::String) at ./loading.jl:576
 [5] include(::String) at ./sysimg.jl:14
while loading /home/shekhar/uavtalk/scripts/test_quat.jl, in expression starting on line 14

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 4, 2018, 4:21pm UTC](https://discourse.julialang.org/t/how-to-broadcast-a-vector-of-an-array/11418/2 "2018-06-04T16:21:06Z")

</div>

Broadcasting works completely elementwise. What you’re after is a function that operates on whole columns of your matrix at a time. Mapping across slices: [mapslices](https://docs.julialang.org/en/stable/stdlib/arrays/#Base.mapslices).

You can also use `reinterpret` here if you want to share memory.

---

<div class="post-metadata">

**Author:** ![bcmichael](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcmichael/32/10003_2.png) [@bcmichael](https://discourse.julialang.org/u/bcmichael)\
**Post date:** [June 4, 2018, 4:48pm UTC](https://discourse.julialang.org/t/how-to-broadcast-a-vector-of-an-array/11418/3 "2018-06-04T16:48:05Z")

</div>

Your Quat function expects an `SVector{4,Float64}`, so to broadcast with it you’ll need some container like a vector that contains `SVector{4,Float64}`'s. In your code q1 and q2 aren’t SVector’s. They’re just normal vectors. The way you are making quat concatenates q1 and q2 into a single 2 dimensional array. You would probably want to make a vector of SVector’s instead.

```julia
q1 = SVector{4,Float64}(0.287819, -0.0776879, 0.208938, 0.931381)
q2 = SVector{4,Float64}(0.287819, -0.0776879, 0.208938, 0.931381)
quat = [q1, q2]
new_quat = Quat.(quat)

```
