# Fill!() vs for loop

**URL:** <https://discourse.julialang.org/t/fill-vs-for-loop/9194>\
**Category:** Data\
**Created:** [February 20, 2018, 7:41am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194 "2018-02-20T07:41:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [February 20, 2018, 7:41am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/1 "2018-02-20T07:41:42Z")

</div>

Technically there shouldnt be a difference between using fill! to make every element of a matrix a SMCSC and using a for loop to do so.  
But using fill!() messes up code. As in it seems like two copies are being made somewhere?

---

<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:** [February 20, 2018, 9:54am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/2 "2018-02-20T09:54:42Z")

</div>

Can you show an example to demonstrate what the problem is?

I can tell you that `fill` will create a new array, while `fill!` will not. For example, `fill!(A, 3)` will replace every element in the array `A` with the value `3`. Another, equally fast, option is `A .= 3`.

---

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [February 20, 2018, 10:08am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/3 "2018-02-20T10:08:19Z")

</div>

Suppose I do:

```julia
A= Matrix{Vector{SparseMatrixCSC{Int8}}}(10,10)

fill!(A, [sparse(zeros(Int8, 10, 10))])
A[1,1][1][1,1] = 30
#gives A[1,1][1][1,1] equal to 60

# ||

for i in eachindex(A)
A[i] = [sparse(zeros(Int8, 10, 10))]
end
A[1,1][1][1,1] = 30
#gives A[1,1][1][1,1] equal to 30

```

After executing with fill() and then the for loop, the fill method adds almost two times the result of the actual assignment.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 20, 2018, 10:12am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/4 "2018-02-20T10:12:30Z")

</div>

That code doesn’t work. Are you sure you run exactly that?

FWIW, `fill` will use the same object for all entries, but the loop will create a new matrix on each call. So that gives very different results.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [February 20, 2018, 10:13am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/5 "2018-02-20T10:13:02Z")

</div>

> [@abshej](#):
>
> `fill(A, [sparse(zeros(Int8, 10, 10))])`

This doesn’t work. What should this do?

---

<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:** [February 20, 2018, 10:15am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/6 "2018-02-20T10:15:08Z")

</div>

> [@abshej](#):
>
> `A= Matrix{Vector{SparseMatrixCSC{Int8}}}(10,10)`

An array of vectors of sparse matrices!? That is quite mind bending. Is that really what you want?

Also `fill(A, ...)` doesn’t work, it just gives an error. You have to use `fill!` here. And after you have done that, `A[1,1] = 30` also fails, and raises an error.

Did you try run the code before you posted it? What version of Julia are you using?

---

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [February 20, 2018, 10:40am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/7 "2018-02-20T10:40:03Z")

</div>

I am sorry, I did use **fill!()**.  
It still doesn’t execute as expected.

> [@DNF](#):
>
> Did you try run the code before you posted it? What version of Julia are you using?

Yes; 0.6.2

---

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [February 20, 2018, 10:44am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/8 "2018-02-20T10:44:11Z")

</div>

This should make every element of A a single element Vector of SparseMatrixCSCs of all zeros.

---

<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:** [February 20, 2018, 10:45am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/9 "2018-02-20T10:45:13Z")

</div>

OK. So what is it that you want `A` to be? Should really it be a matrix of vectors of sparse matrices, or something else?

I’m getting confused because you first make this very complicated array, and then try to do `A[1, 1] = 30`, which cannot work, because `A` is expecting elements to be vectors of sparse matrices, not just a number.

---

<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:** [February 20, 2018, 10:46am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/10 "2018-02-20T10:46:25Z")

</div>

> [@abshej](#):
>
> This should make every element of A a single element Vector of SparseMatrixCSCs of all zeros.

OK, got it. But then what are you trying to achieve by `A[1, 1] = 30`?

---

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [February 20, 2018, 10:47am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/11 "2018-02-20T10:47:20Z")

</div>

Again, sorry. It should be A[1,1][1][1,1]  
It gives incorrect answers using this

---

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [February 20, 2018, 10:49am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/12 "2018-02-20T10:49:03Z")

</div>

Actually, I set multiple elements of A like that using a conditioned loop but the results are incorrect using fill!() at the start

---

<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:** [February 20, 2018, 10:51am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/13 "2018-02-20T10:51:38Z")

</div>

> [@abshej](#):
>
> ```julia
> A= Matrix{Vector{SparseMatrixCSC{Int8}}}(10,10)
> 
> fill!(A, [sparse(zeros(Int8, 10, 10))])
> A[1,1][1][1,1] = 30
> 
> ```

For me the result of this code is that `A[1,1][1][1,1]` equals 30, not 60.

---

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [February 20, 2018, 10:52am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/14 "2018-02-20T10:52:39Z")

</div>

Okay then I’ll check the code logic again.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [February 20, 2018, 10:56am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/15 "2018-02-20T10:56:11Z")

</div>

`fill` (and `fill!`) set every element of their first argument to be the second argument, which is really _not_ what you’d want in this case (I don’t think you want all of the elements of the “filled” vector to all point to the exact same vector of sparse matrices).

In this case, I think array comprehension is your friend.  
`[Vector{SparseMatrixCSC{Int8}}() for i=1:10, j=1:10]`  
should do the trick for you.

---

<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:** [February 20, 2018, 10:56am UTC](https://discourse.julialang.org/t/fill-vs-for-loop/9194/16 "2018-02-20T10:56:58Z")

</div>

BTW, take a look at the help section of `fill!` by typing `?fill!` in the Julia REPL. You are probably running into problems with all elements of `A` holding the same identical array.
