# How to broadcast for 3d-array?

**URL:** <https://discourse.julialang.org/t/how-to-broadcast-for-3d-array/113581>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 28, 2024, 7:33am UTC](https://discourse.julialang.org/t/how-to-broadcast-for-3d-array/113581 "2024-04-28T07:33:47Z")\
**Posts on this page:** 5\
**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:** [April 28, 2024, 7:33am UTC](https://discourse.julialang.org/t/how-to-broadcast-for-3d-array/113581/1 "2024-04-28T07:33:48Z")

</div>

This is a very amateur question, but how can I make the code to output a three-dimensional array when I broadcast to a variable like the one in the image in julia? I want the element specified by [i,j,k] in the three-dimensional array to be the value when the i,j,kth element of each argument is assigned to the function.

 ![スクリーンショット 2024-04-28 16.22.30](https://global.discourse-cdn.com/julialang/original/3X/0/9/09e6e3dfa6274448ea334785382e97ffd7cf087b.png)

---

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [April 28, 2024, 8:18am UTC](https://discourse.julialang.org/t/how-to-broadcast-for-3d-array/113581/2 "2024-04-28T08:18:42Z")

</div>

You might be interested in [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757),  
which shows how to use triple back-quotes to format code.

> I want the element specified by [i,j,k] in the three-dimensional array to be the value when the i,j,kth element of each argument is assigned to the function.

This is something that can be done neatly with [comprehensions](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions)  
rather than broadcasting:

```julia-repl
julia> v = [0.1, 0.2, 0.3, 0.4]

julia> func(a, b, c) = sin(a + b + c)

julia> [func(a, b, c) for a in v, b in v, c in v]
4×4×4 Array{Float64, 3}:
[:, :, 1] =
 0.29552 0.389418 0.479426 0.564642
 0.389418 0.479426 0.564642 0.644218
 0.479426 0.564642 0.644218 0.717356
 0.564642 0.644218 0.717356 0.783327

[:, :, 2] =
 0.389418 0.479426 0.564642 0.644218
 0.479426 0.564642 0.644218 0.717356
 0.564642 0.644218 0.717356 0.783327
 0.644218 0.717356 0.783327 0.841471

[:, :, 3] =
 0.479426 0.564642 0.644218 0.717356
 0.564642 0.644218 0.717356 0.783327
 0.644218 0.717356 0.783327 0.841471
 0.717356 0.783327 0.841471 0.891207

[:, :, 4] =
 0.564642 0.644218 0.717356 0.783327
 0.644218 0.717356 0.783327 0.841471
 0.717356 0.783327 0.841471 0.891207
 0.783327 0.841471 0.891207 0.932039

```

---

<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:** [April 28, 2024, 8:19am UTC](https://discourse.julialang.org/t/how-to-broadcast-for-3d-array/113581/3 "2024-04-28T08:19:04Z")

</div>

You have to reshape the inputs so that one argument lies along the first dimension, the second along the second dimension, and so on. Try

```julia
func.(x, y', reshape(z, 1, 1, :))

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 28, 2024, 1:01pm UTC](https://discourse.julialang.org/t/how-to-broadcast-for-3d-array/113581/4 "2024-04-28T13:01:38Z")

</div>

Here another approach.

```julia-repl
julia> cis = CartesianIndices((4,4,4))
CartesianIndices((4, 4, 4))

julia> Tuple.(cis)
4×4×4 Array{Tuple{Int64, Int64, Int64}, 3}:
[:, :, 1] =
 (1, 1, 1) (1, 2, 1) (1, 3, 1) (1, 4, 1)
 (2, 1, 1) (2, 2, 1) (2, 3, 1) (2, 4, 1)
 (3, 1, 1) (3, 2, 1) (3, 3, 1) (3, 4, 1)
 (4, 1, 1) (4, 2, 1) (4, 3, 1) (4, 4, 1)

[:, :, 2] =
 (1, 1, 2) (1, 2, 2) (1, 3, 2) (1, 4, 2)
 (2, 1, 2) (2, 2, 2) (2, 3, 2) (2, 4, 2)
 (3, 1, 2) (3, 2, 2) (3, 3, 2) (3, 4, 2)
 (4, 1, 2) (4, 2, 2) (4, 3, 2) (4, 4, 2)

[:, :, 3] =
 (1, 1, 3) (1, 2, 3) (1, 3, 3) (1, 4, 3)
 (2, 1, 3) (2, 2, 3) (2, 3, 3) (2, 4, 3)
 (3, 1, 3) (3, 2, 3) (3, 3, 3) (3, 4, 3)
 (4, 1, 3) (4, 2, 3) (4, 3, 3) (4, 4, 3)

[:, :, 4] =
 (1, 1, 4) (1, 2, 4) (1, 3, 4) (1, 4, 4)
 (2, 1, 4) (2, 2, 4) (2, 3, 4) (2, 4, 4)
 (3, 1, 4) (3, 2, 4) (3, 3, 4) (3, 4, 4)
 (4, 1, 4) (4, 2, 4) (4, 3, 4) (4, 4, 4)

julia> Tuple.(cis) .|> ((x,y,z),)->(x/10, y/10, z/10)
4×4×4 Array{Tuple{Float64, Float64, Float64}, 3}:
[:, :, 1] =
 (0.1, 0.1, 0.1) (0.1, 0.2, 0.1) (0.1, 0.3, 0.1) (0.1, 0.4, 0.1)
 (0.2, 0.1, 0.1) (0.2, 0.2, 0.1) (0.2, 0.3, 0.1) (0.2, 0.4, 0.1)
 (0.3, 0.1, 0.1) (0.3, 0.2, 0.1) (0.3, 0.3, 0.1) (0.3, 0.4, 0.1)
 (0.4, 0.1, 0.1) (0.4, 0.2, 0.1) (0.4, 0.3, 0.1) (0.4, 0.4, 0.1)

[:, :, 2] =
 (0.1, 0.1, 0.2) (0.1, 0.2, 0.2) (0.1, 0.3, 0.2) (0.1, 0.4, 0.2)
 (0.2, 0.1, 0.2) (0.2, 0.2, 0.2) (0.2, 0.3, 0.2) (0.2, 0.4, 0.2)
 (0.3, 0.1, 0.2) (0.3, 0.2, 0.2) (0.3, 0.3, 0.2) (0.3, 0.4, 0.2)
 (0.4, 0.1, 0.2) (0.4, 0.2, 0.2) (0.4, 0.3, 0.2) (0.4, 0.4, 0.2)

[:, :, 3] =
 (0.1, 0.1, 0.3) (0.1, 0.2, 0.3) (0.1, 0.3, 0.3) (0.1, 0.4, 0.3)
 (0.2, 0.1, 0.3) (0.2, 0.2, 0.3) (0.2, 0.3, 0.3) (0.2, 0.4, 0.3)
 (0.3, 0.1, 0.3) (0.3, 0.2, 0.3) (0.3, 0.3, 0.3) (0.3, 0.4, 0.3)
 (0.4, 0.1, 0.3) (0.4, 0.2, 0.3) (0.4, 0.3, 0.3) (0.4, 0.4, 0.3)

[:, :, 4] =
 (0.1, 0.1, 0.4) (0.1, 0.2, 0.4) (0.1, 0.3, 0.4) (0.1, 0.4, 0.4)
 (0.2, 0.1, 0.4) (0.2, 0.2, 0.4) (0.2, 0.3, 0.4) (0.2, 0.4, 0.4)
 (0.3, 0.1, 0.4) (0.3, 0.2, 0.4) (0.3, 0.3, 0.4) (0.3, 0.4, 0.4)
 (0.4, 0.1, 0.4) (0.4, 0.2, 0.4) (0.4, 0.3, 0.4) (0.4, 0.4, 0.4)

julia> Tuple.(cis) .|>
           ((x,y,z),) -> (x/10, y/10, z/10) .|>
           (args...) -> sin(+(args...))
4×4×4 Array{Tuple{Float64, Float64, Float64}, 3}:
[:, :, 1] =
 (0.0998334, 0.0998334, 0.0998334) … (0.0998334, 0.389418, 0.0998334)
 (0.198669, 0.0998334, 0.0998334) (0.198669, 0.389418, 0.0998334)
 (0.29552, 0.0998334, 0.0998334) (0.29552, 0.389418, 0.0998334)
 (0.389418, 0.0998334, 0.0998334) (0.389418, 0.389418, 0.0998334)

[:, :, 2] =
 (0.0998334, 0.0998334, 0.198669) … (0.0998334, 0.389418, 0.198669)
 (0.198669, 0.0998334, 0.198669) (0.198669, 0.389418, 0.198669)
 (0.29552, 0.0998334, 0.198669) (0.29552, 0.389418, 0.198669)
 (0.389418, 0.0998334, 0.198669) (0.389418, 0.389418, 0.198669)

[:, :, 3] =
 (0.0998334, 0.0998334, 0.29552) … (0.0998334, 0.389418, 0.29552)
 (0.198669, 0.0998334, 0.29552) (0.198669, 0.389418, 0.29552)
 (0.29552, 0.0998334, 0.29552) (0.29552, 0.389418, 0.29552)
 (0.389418, 0.0998334, 0.29552) (0.389418, 0.389418, 0.29552)

[:, :, 4] =
 (0.0998334, 0.0998334, 0.389418) … (0.0998334, 0.389418, 0.389418)
 (0.198669, 0.0998334, 0.389418) (0.198669, 0.389418, 0.389418)
 (0.29552, 0.0998334, 0.389418) (0.29552, 0.389418, 0.389418)
 (0.389418, 0.0998334, 0.389418) (0.389418, 0.389418, 0.389418)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 28, 2024, 6:07pm UTC](https://discourse.julialang.org/t/how-to-broadcast-for-3d-array/113581/5 "2024-04-28T18:07:42Z")

</div>

```julia

Base.splat(func).(Iterators.product(v,v,v))

```

If your function takes a tuple as input, you don’t need the splat

```julia
funcs((a, b, c))= sin(a+b+c)

funcs.(Iterators.product(v,v,v))

```
