# Calculating Statistics of an Array with A Mask (Masked Array)

**URL:** https://discourse.julialang.org/t/calculating-statistics-of-an-array-with-a-mask-masked-array/120868
**Category:** Performance
**Tags:** statistics, performance, arrays, slack
**Created:** [October 3, 2024, 7:17pm UTC](https://discourse.julialang.org/t/calculating-statistics-of-an-array-with-a-mask-masked-array/120868 "2024-10-03T19:17:56Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [October 3, 2024, 7:25pm UTC](https://discourse.julialang.org/t/calculating-statistics-of-an-array-with-a-mask-masked-array/120868/2 "2024-10-03T19:25:23Z")

</div>

This is a pretty common stumbling block, unfortunately:

> [@Shouldn't \`@views sum(data\[mask\])\` be non-allocating?](https://discourse.julialang.org/t/shouldnt-views-sum-data-mask-be-non-allocating/118135/):
>
> I was surprised to see the following function allocates - is this expected? using BenchmarkTools function f\_viewalloc(data, mask) @views sum(data[mask]) end let N = 1\_000\_000 data = fill(true, N) mask = fill(true, N) @btime f\_viewalloc($data, $mask) end 1.476 ms (2 allocations: 7.63 MiB)

> [@Confused about performance interaction between @view and logical (Boolean) indexing](https://discourse.julialang.org/t/confused-about-performance-interaction-between-view-and-logical-boolean-indexing/23120):
>
> I’m trying to understand the interaction between array views and Boolean indexing. In general, my understanding (and experience thus far) is that views generally increasing performance when you’re not modifying the array slices. For example: x = collect(1:1000) @btime z = x[50:200]; 107.278 ns (1 allocation: 1.33 KiB) @btime z = @view x[50:200]; 19.084 ns (1 allocation: 48 bytes) However, now do the equivalent operations using Boolean indexing, and the performance is reversed: w = (x …

> [@Faster array indexing with a logical array](https://discourse.julialang.org/t/faster-array-indexing-with-a-logical-array/47364):
>
> Hi everyone, Pretty basic question… I was experimenting with indexing arrays with logical arrays. For instance, say I have an array shaped (10000,2) and I need to extract all the indices where the sum across the columns is less than a threshold. What would be the best Julian way to do it? I tried list comprehension but that is slower than the method below MWE: using BenchmarkTools # what I could think of ? function getindicesVectorized(x::Array) return x[getindex.(findall(sum(x, dims=2…

---

_[View the full topic](https://discourse.julialang.org/t/calculating-statistics-of-an-array-with-a-mask-masked-array/120868)._
