# Understanding in-place broadcasting syntax (.=)

**URL:** <https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888>\
**Category:** New to Julia\
**Tags:** broadcast\
**Created:** [February 1, 2023, 4:38pm UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888 "2023-02-01T16:38:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 1, 2023, 4:38pm UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/1 "2023-02-01T16:38:05Z")

</div>

> [@Is there a function to clip an array to a maximum value? e.g. set values in an arr](https://discourse.julialang.org/t/is-there-a-function-to-clip-an-array-to-a-maximum-value-e-g-set-values-in-an-arr/56412/4):
>
> `array .= clamp.(array, lo, hi)`

I guess the dot in front of the equal sign is a typo?

---

<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:** [February 1, 2023, 5:32pm UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/2 "2023-02-01T17:32:58Z")

</div>

No, it is correct, and required to make the operation work in-place.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 1, 2023, 9:32pm UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/3 "2023-02-01T21:32:38Z")

</div>

> [@DNF](#):
>
> No, it is correct, and required to make the operation work in-place.

Ok, this notation is not easy to understand for a beginner. Here my code snippet to understand better:

```julia
array = [1 2.0 3.3; 2.0 7.0 -1.3]; array_A = copy(array)
array_A .= clamp.(array_A, -1.0, 6.0)
array_B = clamp.(array, -1.0, 6.0)
array_C .= clamp.(array, -1.0, 6.0) # ERROR: UndefVarError: array_C not defined

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 1, 2023, 10:00pm UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/4 "2023-02-01T22:00:27Z")

</div>

When you write `x[1] = 1` or `y[:] = [1,2,3]`, you depend on `x` and `y` already being defined as arrays and you’re simply mutating their stored values.

Writing `z .= [1,2,3]` is similar. It looks deceptively similar to `z = [1,2,3]`, but they’re two different things: the former updates the stored values in-place, while the latter (re)assigns the identifier entirely.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 1, 2023, 10:38pm UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/5 "2023-02-01T22:38:36Z")

</div>

@uniment And I guess the `in-place` operation saves time and memory for large arrays?

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 1, 2023, 10:47pm UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/6 "2023-02-01T22:47:37Z")

</div>

Small arrays too 😉

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 2, 2023, 1:12am UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/7 "2023-02-02T01:12:00Z")

</div>

> [@ellocco](#):
>
> And I guess the `in-place` operation saves time and memory for large arrays?

[Performance tips - pre-allocating outputs](https://docs.julialang.org/en/v1/manual/performance-tips/#Pre-allocating-outputs)

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [February 2, 2023, 3:49am UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/8 "2023-02-02T03:49:20Z")

</div>

If you struggle with the dots, I often prefer the equivalent `map` and `map!` which can make the in-place intent a little more explicit.

In this case, it would be written as:

```julia
map!(array, array) do x
    clamp(x, lo, hi)
end

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [February 2, 2023, 4:22am UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/9 "2023-02-02T04:22:32Z")

</div>

In fact, if the array in question is one where the first axis is much smaller than the trailing ones, such as a wide matrix with a handful of rows, then [in-place broadcasting can be much slower than `map!`](https://github.com/JuliaLang/julia/issues/47873)

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 2, 2023, 8:20am UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/10 "2023-02-02T08:20:40Z")

</div>

> [@uniment](#):
>
> Writing `z .= [1,2,3]` is similar. It looks deceptively similar to `z = [1,2,3]`, but they’re two different things: the former updates the stored values in-place, while the latter (re)assigns the identifier entirely.

Except for DataFrames 🤪 (see [What if `df.col .= v` was in-place?](https://discourse.julialang.org/t/what-if-df-col-v-was-in-place/89367))

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 2, 2023, 8:27am UTC](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888/11 "2023-02-02T08:27:50Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/8/4/848dc89c4fa7bf1c99a6b20295088d55a9e9f50e.jpeg)
