# Unexpected map! behaviour

**URL:** <https://discourse.julialang.org/t/unexpected-map-behaviour/24920>\
**Category:** New to Julia\
**Created:** [June 4, 2019, 3:25pm UTC](https://discourse.julialang.org/t/unexpected-map-behaviour/24920 "2019-06-04T15:25:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hak](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@hak](https://discourse.julialang.org/u/hak)\
**Post date:** [June 4, 2019, 3:25pm UTC](https://discourse.julialang.org/t/unexpected-map-behaviour/24920/1 "2019-06-04T15:25:50Z")

</div>

Frankly I do not understand this behaviour:

julia\> a = zeros(2,3);  
julia\> map!(x -\>x\*2, a[1,:], [1,2,3]);  
julia\> a  
2×3 Array{Float64,2}:  
0.0 0.0 0.0  
0.0 0.0 0.0

but

julia\> a[1,:]=map(x -\>x\*2, [1,2,3]);  
julia\> a  
2×3 Array{Float64,2}:  
2.0 4.0 6.0  
0.0 0.0 0.0

am I missing something?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 4, 2019, 3:31pm UTC](https://discourse.julialang.org/t/unexpected-map-behaviour/24920/2 "2019-06-04T15:31:36Z")

</div>

In `map!(x ->x*2, a[1,:], [1,2,3])` you are not modifying `a`. `a[1, :]` returns a _new_ vector which you put the result in. It is equivalent to

```julia
a = zeros(2, 3)
tmp = a[1, :]
map!(x->x*2, tmp, [1, 2, 3])

```

so you are only modifying `tmp`. You can use a view if you want your output vector to point to the same memory as `a`, e.g.

```julia
map!(x->x*2, @view(a[1,:]), [1, 2, 3])

```

See also [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

<div class="post-metadata">

**Author:** ![hak](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@hak](https://discourse.julialang.org/u/hak)\
**Post date:** [June 4, 2019, 3:35pm UTC](https://discourse.julialang.org/t/unexpected-map-behaviour/24920/3 "2019-06-04T15:35:27Z")

</div>

Thx a lot!
