# Logical masking of gridded data

**URL:** <https://discourse.julialang.org/t/logical-masking-of-gridded-data/73082>\
**Category:** New to Julia\
**Created:** [December 14, 2021, 3:28pm UTC](https://discourse.julialang.org/t/logical-masking-of-gridded-data/73082 "2021-12-14T15:28:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fran94](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fran94/32/50651_2.png) [@fran94](https://discourse.julialang.org/u/fran94)\
**Post date:** [December 14, 2021, 3:28pm UTC](https://discourse.julialang.org/t/logical-masking-of-gridded-data/73082/1 "2021-12-14T15:28:18Z")

</div>

Hi all,  
I have a regular grid of Nx \* Ny, over which I am computing some things. Now, I want to select a circle on it at location (Nx/4, Ny/2) with radius (Ny/4). I have tried the following:

```julia
circle = @. [((x - Nx/4)^2 + (y - Ny/2)^2 < (Ny/4)^2) for x in 1:Nx, y in 1:Ny]

```

If I understood correctly, I don’t need to meshgrid because of broadcasting, right? However, if I try to plot some data on this with a heatmap, e.g.:

```julia
#u is a vector of size (Nx, Ny) with random data
u[circle] .= NaN
heatmap(u)

```

I get the following picture:  
 ![plot_1](https://global.discourse-cdn.com/julialang/original/3X/a/a/aa94f4787a631f1a260e9247acb88d9b6a84054a.png)  
Any idea why? Thanks!

Francesco

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 14, 2021, 4:01pm UTC](https://discourse.julialang.org/t/logical-masking-of-gridded-data/73082/2 "2021-12-14T16:01:08Z")

</div>

Adding `@.` to your array comprehension doesn’t do anything:

```julia
julia> [i > j for i in 1:2, j in 1:3]
2×3 Matrix{Bool}:
 0 0 0
 1 0 0

julia> @. [i > j for i in 1:2, j in 1:3]
2×3 Matrix{Bool}:
 0 0 0
 1 0 0

```

If you want to use broadcasting directly, you could do something like this:

```julia
circle = @. hypot((1:Nx) - Nx/4, (1:Ny)' - Ny/2) < (Ny/4)

```

…which looks fine to me when plotted

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/7/f7222fb7ce419571196d75f0b1053394c4fe0c45.jpeg)

---

<div class="post-metadata">

**Author:** ![fran94](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fran94/32/50651_2.png) [@fran94](https://discourse.julialang.org/u/fran94)\
**Post date:** [December 14, 2021, 5:50pm UTC](https://discourse.julialang.org/t/logical-masking-of-gridded-data/73082/3 "2021-12-14T17:50:21Z")

</div>

Thank you, this solves my issue! So I think my problem was mostly about not adjointing the y vector, which totally makes sense. Cool use of hypot too, I did not know this function. Thanks!
