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 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, the entry a_{1,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 (or colex) orders, respectively. For example, the array :A = a_{y,x} = \begin{bmatrix} \color{Blue} a_{11} & \color{Blue} a_{12} & \color{Blue} a_{13} \\ \color{Orange} a_{21} & \color{Orange} a_{22} & \color{Orange} a_{23} \end{bmatrix} could be stored in two possible ways: Programming languages handle this in different ways. In
C, multidimensional arrays are stored in row-major order, and the array indexes are written row-first (lexicographical access order): On the other hand, in
Fortran, arrays are stored in column-major order, while the array indexes are still written row-first (colexicographical access order): Note how the use of A[i][j] with multi-step indexing as in C, as opposed to a neutral notation like A(i,j) as in Fortran, almost inevitably implies row-major order for syntactic reasons, so to speak, because it can be rewritten as (A[i])[j], and the A[i] row part can even be assigned to an intermediate variable that is then indexed in a separate expression. (No other implications should be assumed, e.g., Fortran is not column-major simply
because of its notation, and even the above implication could intentionally be circumvented in a new language.) 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 (using the first index for the column and the second index for the row), 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 (forgetting to also invert matrix multiplication order, reverting to convention during code maintenance, etc.), 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. Running the loop row-wise is preferred in row-major languages like C and vice versa for column-major languages. ==Programming languages and libraries==