# How to combine @. with broadcasting for 3d array?

**URL:** <https://discourse.julialang.org/t/how-to-combine-with-broadcasting-for-3d-array/114291>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 15, 2024, 9:00am UTC](https://discourse.julialang.org/t/how-to-combine-with-broadcasting-for-3d-array/114291 "2024-05-15T09:00:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kohei\_Hattori](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kohei_hattori/32/202897_2.png) [@Kohei\_Hattori](https://discourse.julialang.org/u/Kohei_Hattori)\
**Post date:** [May 15, 2024, 9:00am UTC](https://discourse.julialang.org/t/how-to-combine-with-broadcasting-for-3d-array/114291/1 "2024-05-15T09:00:27Z")

</div>

I’m new to Julia.

I want to develop code that combines broadcasting with the @macro for a 3D array like the one below. How can I solve this?

```julia
function func(a,b,c)
    return sin(a+b+c)
end
func.([0.1, 0.2, 0.3, 0.4], [0.1, 0.2, 0.3, 0.4], [0.1, 0.2, 0.3, 0.4])
@. func.(sys.k_mesh, sys.k_mesh', reshape(sys.k_mesh, 1, 1, :))

```

By the way, when I use the following code, I get this error:

```julia
MethodError: no method matching reshape(::Float64, ::Int64, ::Int64, ::Colon)

Closest candidates are:
  reshape(!Matched::OffsetArrays.OffsetArray, ::Union{Colon, Int64}...)
   @ OffsetArrays ~/.julia/packages/OffsetArrays/rMTtC/src/OffsetArrays.jl:383
  reshape(!Matched::AbstractArray, ::Union{Colon, Int64}...)
   @ Base reshapedarray.jl:118
  reshape(!Matched::AbstractArray, ::Union{Colon, Integer, AbstractUnitRange}...)
   @ OffsetArrays ~/.julia/packages/OffsetArrays/rMTtC/src/OffsetArrays.jl:351
  ...

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 15, 2024, 11:38am UTC](https://discourse.julialang.org/t/how-to-combine-with-broadcasting-for-3d-array/114291/2 "2024-05-15T11:38:13Z")

</div>

You are applying `reshape` per-element of `sys.k_mesh`, that’s why the error says you can’t reshape a `Float64`. The docstring for `@.` notes:

```julia
If you want to avoid adding dots for selected function calls in expr, splice those function calls in with $. For example, @. sqrt(abs($sort(x))) is equivalent to sqrt.(abs.(sort(x))) (no
  dot for sort).

```

---

<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:** [May 15, 2024, 12:50pm UTC](https://discourse.julialang.org/t/how-to-combine-with-broadcasting-for-3d-array/114291/3 "2024-05-15T12:50:24Z")

</div>

Note that `@.` is just a tool to apply broadcasting to every call in an expression, but it is not _required_ for broadcasting. Dots alone are sufficient.

> [@Kohei\_Hattori](#):
>
> `@. func.(sys.k_mesh, sys.k_mesh', reshape(sys.k_mesh, 1, 1, :))`

What you wrote would work if you delete the `@.`. You already broadcasted `func` by writing the “`.`” in `func.(...)`, so the `@.` is not needed (and the `@.` is broadcasting the `reshape`, which you didn’t intend).

The vast majority of the time, I just apply dots manually to the parts I want and don’t use `@.`.
