# Mapping vector elements

**URL:** <https://discourse.julialang.org/t/mapping-vector-elements/101659>\
**Category:** New to Julia\
**Tags:** question, zygote\
**Created:** [July 15, 2023, 5:59pm UTC](https://discourse.julialang.org/t/mapping-vector-elements/101659 "2023-07-15T17:59:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)\
**Post date:** [July 15, 2023, 5:59pm UTC](https://discourse.julialang.org/t/mapping-vector-elements/101659/1 "2023-07-15T17:59:24Z")

</div>

Hi, I am defining the gradients by using below code but i want to make negative values to zero. I wrote below code but it is not working. could you please help me to modify this

```julia
function gf_p(p0::Vector, grad::Vector; r, β, η, fem_params)
    if length(grad) > 0
        dgdp, = Zygote.gradient(p -> gf_p(p; r, β, η, fem_params), p0)
        grad[:] = dgdp
    end
    map(x-> grad[:]<0 , 0)
    -grad
end

```

thank you

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 15, 2023, 6:05pm UTC](https://discourse.julialang.org/t/mapping-vector-elements/101659/2 "2023-07-15T18:05:59Z")

</div>

> [@mary](#):
>
> `map(x-> grad[:]<0 , 0)`

Among other oddities, you’re discarding the value of this expression.

---

<div class="post-metadata">

**Author:** ![Alexander\_Knudson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_knudson/32/215656_2.png) [@Alexander\_Knudson](https://discourse.julialang.org/u/Alexander_Knudson)\
**Post date:** [July 15, 2023, 6:33pm UTC](https://discourse.julialang.org/t/mapping-vector-elements/101659/3 "2023-07-15T18:33:40Z")

</div>

One way to set negative values to zero is with the code

```julia
x[x .< 0] .= 0

```

`map` on its own will not modify the values in place.

---

<div class="post-metadata">

**Author:** ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)\
**Post date:** [July 15, 2023, 6:38pm UTC](https://discourse.julialang.org/t/mapping-vector-elements/101659/4 "2023-07-15T18:38:41Z")

</div>

thank you very much! it is working.

---

<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:** [July 15, 2023, 9:48pm UTC](https://discourse.julialang.org/t/mapping-vector-elements/101659/5 "2023-07-15T21:48:31Z")

</div>

You could also do

```julia
x .= max.(x, 0)

```

which should be faster, since it doesn’t create any temporary array.

And this

> [@mary](#):
>
> `grad[:] = dgdp`

should probably be replaced with

```julia
grad .= dgdp

```

Actually, why do you need to modify `grad`? Isn’t it better to just work on `dgdp` without copying the data into `grad`?

---

<div class="post-metadata">

**Author:** ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)\
**Post date:** [July 15, 2023, 10:33pm UTC](https://discourse.julialang.org/t/mapping-vector-elements/101659/6 "2023-07-15T22:33:54Z")

</div>

what do you mean by modifying dgdp? It seems that there are some errors in calculating the gradients by zygot and they are very small so i want to make them as zero.
