How to `fill` a part of the array?

array

#1

For example, I have an array

julia> a = ones(2, 3)
2×3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0

Now I want to fill its first row to be all 3.0,

julia> fill!(a[1,:],3)
3-element Array{Float64,1}:
 3.0
 3.0
 3.0

After this, my a is still not changed. I found this is because the source code only returns a new array, rather than actually fills an array.

function fill!(A::AbstractArray{T}, x) where T
    xT = convert(T, x)
    for I in eachindex(A)
        @inbounds A[I] = xT
    end
    A
end

What should I do to achieve my goal?


#2

This:

julia> a = ones(2, 3)
2×3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0

julia> a[1,:] .= 3
3-element view(::Array{Float64,2}, 1, :) with eltype Float64:
 3.0
 3.0
 3.0

julia> a
2×3 Array{Float64,2}:
 3.0  3.0  3.0
 1.0  1.0  1.0

Your a[1,:] creates a new array. If you want to use fill, this works:

julia> fill!(@view(a[1,:]), 4)
3-element view(::Array{Float64,2}, 1, :) with eltype Float64:
 4.0
 4.0
 4.0

julia> a
2×3 Array{Float64,2}:
 4.0  4.0  4.0
 1.0  1.0  1.0