# Mesh grids and position array tranform

**URL:** <https://discourse.julialang.org/t/mesh-grids-and-position-array-tranform/129983>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 18, 2025, 9:33am UTC](https://discourse.julialang.org/t/mesh-grids-and-position-array-tranform/129983 "2025-06-18T09:33:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyan](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cyan](https://discourse.julialang.org/u/Cyan)\
**Post date:** [June 18, 2025, 9:33am UTC](https://discourse.julialang.org/t/mesh-grids-and-position-array-tranform/129983/1 "2025-06-18T09:33:15Z")

</div>

I am working on the mesh grids, I want to ask whether there is quick transformation and inverse transformation between array\_x and flat\_x?

```julia
array_x = [[1, 1, 1, 1],
            [2, 2, 2, 2],
            [3, 3, 3, 3]]

```

and  
`flat_x = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]`

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [June 18, 2025, 10:08am UTC](https://discourse.julialang.org/t/mesh-grids-and-position-array-tranform/129983/2 "2025-06-18T10:08:47Z")

</div>

Going to `flat_x` you can use

```julia-repl
julia> reduce(vcat, array_x)
12-element Vector{Int64}:
 1
 1
 1
 1
 2
 2
 2
 2
 3
 3
 3
 3

```

which will allocate a new (contiguous) array. There might be non-allocating approaches, but then you would be working with non-contiguous memory, which might also not be ideal.  
For the converse direction you can use

```julia-repl
julia> eachrow(reshape(flat_x, 4, 3)')
3-element RowSlices{LinearAlgebra.Adjoint{Int64, Matrix{Int64}}, Tuple{Base.OneTo{Int64}}, SubArray{Int64, 1, LinearAlgebra.Adjoint{Int64, Matrix{Int64}}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, false}}:
 [1, 1, 1, 1]
 [2, 2, 2, 2]
 [3, 3, 3, 3]

```

or if the number of columns is fixed, statically known, and relatively small

```julia-repl
julia> using StaticArrays

julia> reinterpret(SVector{4, Int64}, flat_x)
3-element reinterpret(SVector{4, Int64}, ::Vector{Int64}):
 [1, 1, 1, 1]
 [2, 2, 2, 2]
 [3, 3, 3, 3]

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 18, 2025, 10:25am UTC](https://discourse.julialang.org/t/mesh-grids-and-position-array-tranform/129983/3 "2025-06-18T10:25:10Z")

</div>

In my experience, you almost never need to create mesh grids like that, with lots of redundant values. What do you need it for?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 18, 2025, 12:34pm UTC](https://discourse.julialang.org/t/mesh-grids-and-position-array-tranform/129983/4 "2025-06-18T12:34:43Z")

</div>

> [@Cyan](#):
>
> `array_x = [[1, 1, 1, 1],` …

Note that Julia has 2d arrays — you don’t generally use arrays of arrays for this purpose like you would in Python syntax. (Also, you can often avoid “mesh-grid” arrays using broadcasting etc., as @DNF alluded to.)
