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

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!!

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

1 Like

See PermutedDimsArray if you’re interested in this option.