# Converting row major to column major in 3 (or more) dimensions

**URL:** <https://discourse.julialang.org/t/converting-row-major-to-column-major-in-3-or-more-dimensions/94434>\
**Category:** General Usage\
**Created:** [February 10, 2023, 8:47pm UTC](https://discourse.julialang.org/t/converting-row-major-to-column-major-in-3-or-more-dimensions/94434 "2023-02-10T20:47:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Aronoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robert_aronoff/32/36918_2.png) [@Robert\_Aronoff](https://discourse.julialang.org/u/Robert_Aronoff)\
**Post date:** [February 10, 2023, 8:47pm UTC](https://discourse.julialang.org/t/converting-row-major-to-column-major-in-3-or-more-dimensions/94434/1 "2023-02-10T20:47:26Z")

</div>

I have a use case where I receive an array (pointer) back from a C library. The C array is converted to a Julia array with unsafe\_wrap. Every thing is fine with 1 and 2 dimensional arrays. Unsafe\_wrap assumes the source arrays are column major. However, the C arrays are row major and 2 dimensional arrays need both a reshape and then transpose in order for row, column references to be accurate. This is just 2 lines of code and works just fine. I am getting turned around in how to do this in 3 (or more) dimensions. Fundamentally, I am not certain how arrays of 3+ dimensions are stored in memory.

Is there any function ( or package) that can help me with this mapping conversion. The array sizes can be substantial and need something efficient in time and memory.

Thank you!!

---

<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:** [February 10, 2023, 8:55pm UTC](https://discourse.julialang.org/t/converting-row-major-to-column-major-in-3-or-more-dimensions/94434/2 "2023-02-10T20:55:42Z")

</div>

> [@Robert\_Aronoff](#):
>
> I am getting turned around in how to do this in 3 (or more) dimensions. Fundamentally, I am not certain how arrays of 3+ dimensions are stored in memory.

Just use `permutedims(A, reverse(1:ndims(A)))`. Row major vs. column major in _any_ dimensions is exactly equivalent to reversing the order of the axes.

(Or leave the array as-is and just reverse the order of the indices in how you use it.)

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [February 10, 2023, 10:05pm UTC](https://discourse.julialang.org/t/converting-row-major-to-column-major-in-3-or-more-dimensions/94434/3 "2023-02-10T22:05:06Z")

</div>

> [@stevengj](#):
>
> (Or leave the array as-is and just reverse the order of the indices in how you use it.)

See [`PermutedDimsArray`](https://docs.julialang.org/en/v1/base/arrays/#Base.PermutedDimsArrays.PermutedDimsArray) if you’re interested in this option.
