# Is there a function to clip an array to a maximum value? e.g. set values in an arr

**URL:** 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
**Category:** General Usage
**Created:** [March 3, 2021, 3:19pm UTC](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 "2021-03-03T15:19:17Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)
#### Post date: [March 3, 2021, 3:19pm UTC](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/1 "2021-03-03T15:19:17Z")

</div>

Is there a function to clip an array to a maximum value? e.g. set values in an array greater than 1.0 to 1.0

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1614784736271200?thread_ts=1614784736.271200&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 3, 2021, 4:34pm UTC](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/2 "2021-03-03T16:34:55Z")

</div>

Yup, it’s called `clamp!`:

```julia
help?> clamp!
search: clamp! clamp

  clamp!(array::AbstractArray, lo, hi)

  Restrict values in array to the specified range, in-place. See also clamp.

```

---

<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: [March 3, 2021, 5:56pm UTC](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/3 "2021-03-03T17:56:53Z")

</div>

Just for clamping the maximum, and not the minimum, you can use

```julia
x .= min.(x, hi) 

```

---

<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: [March 3, 2021, 6:00pm UTC](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 "2021-03-03T18:00:32Z")

</div>

> [@rdeits](#):
>
> Yup, it’s called `clamp!`

I wonder why we kept the `clamp!` function, given that it seems equivalent to

```julia
array .= clamp.(array, lo, hi)

```

Probably it was just an oversight that it didn’t get removed when we removed the other vectorized functions in favor of dot calls?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 3, 2021, 6:12pm UTC](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/5 "2021-03-03T18:12:23Z")

</div>

Yeah, I was actually surprised to find it as well–it does feel like a vestige from the era before broadcasting.

---

<div class="post-metadata">

### Author: ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)
#### Post date: [March 3, 2021, 7:33pm UTC](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/6 "2021-03-03T19:33:03Z")

</div>

The redundancy has been mentioned before in [stackoverflow](https://stackoverflow.com/questions/51987478/why-have-clamp-clamp-and-clamp-in-julia).

Curiously, I get different timing for `clamp` vs. `clamp!`:

```julia
using BenchmarkTools
X0 = randn(100,100); X = copy(X0);
@btime clamp!($X, -0.5, 0.5);
# 1.803 μs (0 allocations: 0 bytes)
clampme!(X, lo, hi) = X .= clamp.(X, lo, hi)
X = copy(X0);
@btime clampme!($X, -0.5, 0.5);
# 9.515 μs (0 allocations: 0 bytes)

```

It seems that `clamp!` does an `@inbounds` loop, and perhaps broadcasting fails to do it automatically. Or perhaps this is user benchmarking error.

---

<div class="post-metadata">

### Author: ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)
#### Post date: [March 3, 2021, 11:49pm UTC](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/7 "2021-03-03T23:49:57Z")

</div>

I think slowness may be related to this [issue](https://github.com/JuliaLang/julia/issues/28126)

---

<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: [March 4, 2021, 1:30am UTC](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/8 "2021-03-04T01:30:50Z")

</div>

I don’t think so, since that issue is for 1d arrays, but in this case it’s almost the same speed for 1d arrays:

```julia
julia> X = randn(100*100);

julia> @btime clamp!($X, -0.5, 0.5);
  1.723 μs (0 allocations: 0 bytes)

julia> @btime clampme!($X, -0.5, 0.5);
  1.721 μs (0 allocations: 0 bytes)

```

It seems to be specific to broadcast loops for 2d (or more) arrays.

---

<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 1, 2023, 9:36pm UTC](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/9 "2023-02-01T21:36:10Z")

</div>

3 posts were split to a new topic: [Understanding in-place broadcasting syntax (.=)](https://discourse.julialang.org/t/understanding-in-place-broadcasting-syntax/93888)

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [February 1, 2023, 5:59pm UTC](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/11 "2023-02-01T17:59:38Z")

</div>

I think this is [43153](https://github.com/JuliaLang/julia/issues/43153), broadcasts which re-use an array.
