# Replacing missing values in a matrix is super slow

**URL:** <https://discourse.julialang.org/t/replacing-missing-values-in-a-matrix-is-super-slow/73213>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 16, 2021, 4:22pm UTC](https://discourse.julialang.org/t/replacing-missing-values-in-a-matrix-is-super-slow/73213 "2021-12-16T16:22:36Z")\
**Posts on this page:** 1\
**Showing post:** 38

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [December 17, 2021, 12:36am UTC](https://discourse.julialang.org/t/replacing-missing-values-in-a-matrix-is-super-slow/73213/38 "2021-12-17T00:36:47Z")

</div>

But even the first run should not allocate an array of the full size.  
Everything is done in-place.

See this, that works but is doing the same!

```julia
julia> capnan0!(A,B,v) = B[A .> v] .= NaN
capnan0! (generic function with 2 methods)

julia> A = randn((128, 512, 256));

julia> B = copy(A);

julia> capnan0!(A, B, 100);

julia> @time capnan0!(A, B, 100); A .= B; @time capnan0!(A, B, 100); A .= B; @time capnan0!(A, B, 100); #
  0.018046 seconds (8 allocations: 2.004 MiB)
  0.014085 seconds (8 allocations: 2.004 MiB)
  0.014415 seconds (8 allocations: 2.004 MiB)

```

There are some [aliasing bugs](https://discourse.julialang.org/t/memory-allocation-with-view-and-array-assignment/67109) which might occur also here?

EDIT: The mechanism maybe works in the second run since we do no assignments then.

---

_[View the full topic](https://discourse.julialang.org/t/replacing-missing-values-in-a-matrix-is-super-slow/73213)._
