How to check if my gridded X and Y are inside a polygon?

I think your understanding is correct.

Basically ndgrid creates a matrix for both longitude and latitude. That way, the program knows the exact longitude and latitude values at each of the grid points.

That said, I agree the Julia way is more efficient.