Row- and column-major order


In computing, row-major order and column-major order are methods for storing multidimensional arrays in linear storage such as random access memory.
The difference between the orders lies in which elements of an array are contiguous in memory. In row-major order, the consecutive elements of a row reside next to each other, whereas the same holds true for consecutive elements of a column in column-major order. While the terms allude to the rows and columns of a two-dimensional array, i.e. a matrix, the orders can be generalized to arrays of any dimension by noting that the terms row-major and column-major are equivalent to lexicographic and colexicographic orders, respectively.
Data layout is critical for correctly passing arrays between programs written in different programming languages. It is also important for performance when traversing an array because modern CPUs process sequential data more efficiently than nonsequential data. This is primarily due to CPU caching. In addition, contiguous access makes it possible to use SIMD instructions that operate on vectors of data. In some media such as tape or NAND flash memory, accessing sequentially is orders of magnitude faster than nonsequential access.

Explanation and example

The terms row-major and column-major stem from the terminology related to ordering objects. A general way to order objects with many attributes is to the first group and order them by one attribute, and then, within each such group, group and order them by another attribute, etc. If more than one attribute participates in ordering, the first would be called major and the last minor. If two attributes participate in ordering, it is sufficient to name only the major attribute.
In the case of arrays, the attributes are the indices along each dimension. For matrices in mathematical notation, the first index indicates the row, and the second indicates the column, e.g., given a matrix A, a1,2 is in its first row and second column. This convention is carried over to the syntax in programming languages, although often with indexes starting at 0 instead of 1.
Even though the row is indicated by the first index and the column by the second index, no grouping order between the dimensions is implied by this. The choice of how to group and order the indices, either by row-major or column-major methods, is thus a matter of convention. The same terminology can be applied to even higher dimensional arrays. Row-major grouping starts from the leftmost index and column-major from the rightmost index, leading to lexicographic and colexicographic orders, respectively.
For example, for the array

the two possible ways are

AddressAccessValue
0A
1A
2A
3A
4A
5A

AddressAccessValue
0A
1A
2A
3A
4A
5A

The two possible ways :

AddressAccessValue
1A
2A
3A
4A
5A
6A

AddressAccessValue
1A
2A
3A
4A
5A
6A

Note how the use of A with multi-step indexing as in C, as opposed to a neutral notation like A as in Fortran, almost inevitably implies row-major order for syntactic reasons, so to speak, because it can be rewritten as , and the A row part can even be assigned to an intermediate variable that is then indexed in a separate expression.
To use column-major order in a row-major environment, or vice versa, for whatever reason, one workaround is to assign non-conventional roles to the indexes, and another is to bypass language syntax by explicitly computing positions in a one-dimensional array. Of course, deviating from convention probably incurs a cost that increases with the degree of necessary interaction with conventional language features and other code, not only in the form of increased vulnerability to mistakes, but also in the form of having to actively rearrange elements, all of which have to be weighed against any original purpose such as increasing performance.

Programming languages and libraries

Programming languages or their standard libraries that support multi-dimensional arrays typically have a native row-major or column-major storage order for these arrays.
Row-major order is used in C/C++/Objective-C, PL/I, Pascal, Speakeasy, SAS, and Rasdaman.
Column-major order is used in Fortran, MATLAB, GNU Octave, S-Plus, R, Julia, and Scilab.
A special case would be OpenGL for graphics processing. Since "recent mathematical treatments of linear algebra and related fields invariably treat vectors as columns," designer Mark Segal decided to substitute this for the convention in predecessor IRIS GL, which was to write vectors as rows; for compatibility, transformation matrices would still be stored in vector-major rather than coordinate-major order, and he then used the "subterfuge say that matrices in OpenGL are stored in column-major order". This was really only relevant for presentation, because matrix multiplication was stack-based and could still be interpreted as post-multiplication, but, worse, reality leaked through the C-based API because individual elements would be accessed as M or, effectively, M, which unfortunately muddled the convention that the designer sought to adopt, and this was even preserved in the OpenGL Shading Language that was later added. As a result, many developers will now simply declare that having the column as the first index is the definition of column-major, even though this is clearly not the case with a real column-major language like Fortran.

Neither row-major nor column-major

A typical alternative for dense array storage is to use Iliffe vectors, which typically store elements in the same row contiguously, but not the rows themselves. They are used in : Java, C#/CLI/.Net, Scala, and Swift.
Even less dense is to use lists of lists, e.g., in Python, and in the Wolfram Language of Wolfram Mathematica.
An alternative approach uses tables of tables, e.g., in Lua.

External libraries

Support for multi-dimensional arrays may also be provided by external libraries, which may even support arbitrary orderings, where each dimension has a stride value, and row-major or column-major are just two possible resulting interpretations.
Row-major order is the default in NumPy.
Column-major order is the default in Eigen and .
Torch changed from column-major to row-major default order.

Transposition

As exchanging the indices of an array is the essence of array transposition, an array stored as row-major but read as column-major will appear transposed. As actually performing this rearrangement in memory is typically an expensive operation, some systems provide options to specify individual matrices as being stored transposed. The programmer must then decide whether or not to rearrange the elements in memory, based on the actual usage.
For example, the Basic Linear Algebra Subprograms functions are passed flags indicating which arrays are transposed.

Address calculation in general

The concept generalizes to arrays with more than two dimensions.
For a d-dimensional array with dimensions Nk, a given element of this array is specified by a tuple of d indices.
In row-major order, the last dimension is contiguous, so that the memory-offset of this element is given by:
In column-major order, the first dimension is contiguous, so that the memory-offset of this element is given by:
where the empty product is the multiplicative identity element, i.e.,.
For a given order, the stride in dimension k is given by the multiplication value in parentheses before index nk in the right hand-side summations above.
More generally, there are d! possible orders for a given array, one for each permutation of dimensions, although the lists of stride values are not necessarily permutations of each other, e.g., in the 2-by-3 example above, the strides are for row-major and for column-major.