hak
1
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?
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
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.
map!(x->x*2, @view(a[1,:]), [1, 2, 3])
See also PSA: how to quote code with backticks
3 Likes