# Undef to zeros?

**URL:** <https://discourse.julialang.org/t/undef-to-zeros/64871>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 18, 2021, 7:12pm UTC](https://discourse.julialang.org/t/undef-to-zeros/64871 "2021-07-18T19:12:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marianoarnaiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marianoarnaiz/32/19377_2.png) [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Post date:** [July 18, 2021, 7:12pm UTC](https://discourse.julialang.org/t/undef-to-zeros/64871/1 "2021-07-18T19:12:12Z")

</div>

Hi all.  
I have this array that was a lot info on it. Some of the cells in the matrix are "#undef "  
I want to write the file to ascii… any way to change all the #undef to zeros, or something?  
Thanks!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 18, 2021, 7:33pm UTC](https://discourse.julialang.org/t/undef-to-zeros/64871/2 "2021-07-18T19:33:29Z")

</div>

you can achieve this by initializing the matrix to be all zeros instead of trying to find and replace `#undef`. How did you obtain such a matrix to begin with?

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [July 18, 2021, 7:37pm UTC](https://discourse.julialang.org/t/undef-to-zeros/64871/3 "2021-07-18T19:37:39Z")

</div>

Something like this?

```julia
for i in eachindex(x)
    if !isassigned(x, i)
        x[i] = 0
    end
end

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 18, 2021, 7:38pm UTC](https://discourse.julialang.org/t/undef-to-zeros/64871/4 "2021-07-18T19:38:08Z")

</div>

you might want `zero(eltype(x))`

---

<div class="post-metadata">

**Author:** ![marianoarnaiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marianoarnaiz/32/19377_2.png) [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Post date:** [July 19, 2021, 9:08am UTC](https://discourse.julialang.org/t/undef-to-zeros/64871/5 "2021-07-19T09:08:13Z")

</div>

Perfect! Thanks!
