Logical masking of gridded data

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:

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.:

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

I get the following picture:
plot_1
Any idea why? Thanks!

Francesco

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

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:

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

…which looks fine to me when plotted

3 Likes

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!