# How to \`fill\` a part of the array?

**URL:** <https://discourse.julialang.org/t/how-to-fill-a-part-of-the-array/18868>\
**Category:** General Usage\
**Tags:** array\
**Created:** [December 20, 2018, 8:22pm UTC](https://discourse.julialang.org/t/how-to-fill-a-part-of-the-array/18868 "2018-12-20T20:22:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [December 20, 2018, 8:22pm UTC](https://discourse.julialang.org/t/how-to-fill-a-part-of-the-array/18868/1 "2018-12-20T20:22:37Z")

</div>

For example, I have an array

```julia
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
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 `fill`s an array.

```julia
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?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 20, 2018, 8:28pm UTC](https://discourse.julialang.org/t/how-to-fill-a-part-of-the-array/18868/2 "2018-12-20T20:28:21Z")

</div>

This:

```julia
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
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

```
