4
$\begingroup$

I have this simple question but I am trying to figure out why:

Are matrices stored column-wise in MATLAB? If so then why?

I theorize that they are stored column-wise because the memory does not have a rectangular structure but rather stores data as bits. I can't find any details on MATLAB's forum regarding this matter. So I would hope someone can provide me with more information.

$\endgroup$
5
  • 1
    $\begingroup$ MATLAB is column-major, it is not very hard to find it in the documentation once you know what to look for: mathworks.com/help/matlab/matlab_external/… . For programming language developers, column-major or row-major layout of data is a design choice, and neither one has significant advantages over the other. However, MATLAB is a programming language based on linear algebra and mathematicians usually think in terms of column vectors, so I would argue that it is more natural. See en.wikipedia.org/wiki/Row-_and_column-major_order for more discussion. $\endgroup$ May 9, 2021 at 5:57
  • $\begingroup$ Alright thank you very much! @AbdullahAliSivas $\endgroup$
    – SPARSE
    May 9, 2021 at 9:34
  • $\begingroup$ I might argue that for most programmers today, row-major is more "natural". $\endgroup$ May 9, 2021 at 15:21
  • 1
    $\begingroup$ @AbdullahAliSivas, could you expand your comment into answer? $\endgroup$
    – nicoguaro
    May 9, 2021 at 15:54
  • $\begingroup$ Also, the origin of Matlab was a script interface to some of the numerical libraries in the netlib collection, starting with LAPACK, and these were mostly Fortran codes. Fortran is column major probably due to the same reason as above. $\endgroup$ May 12, 2021 at 6:19

1 Answer 1

7
$\begingroup$

This is my comment expanded into an answer.

MATLAB is a column-major programming language; it is not very hard to find it in the documentation once you know what to look for:

https://www.mathworks.com/help/matlab/matlab_external/matlab-data.html#f22019

The layout of data structures (most commonly row-major or column-major) used in a programming language is decided at the beginning of the development process by the developers of the programming language. It is generally a matter of taste, as neither one has significant advantages over the other. However, this choice has potential consequences in terms of performance to the user; accessing memory locations contiguously is much faster than accessing with (potentially large) strides.

MATLAB is a programming language (initially) designed to do linear algebra operations/research and the main data structure is a matrix. Almost everything in MATLAB is a matrix; for example, scalars are 1-by-1 matrices. In addition, mathematicians usually think in terms of column vectors when they do linear algebra, so I would argue that it was the natural choice for MATLAB.

I agree with Wolfgang Bangerth that the row-major layout may be more natural for most programmers and for most programming languages, but I think it is because how we educated the programmers for the last decade. Python replaced C/C++ (row-major) and Fortran (column-major) in Intro to Programming classes. So the one of the most popular column-major programming languages is now considered to be "old" and "unusable". Also, MATLAB is neither free nor open-source, and it is designed to be a mathematical programming language not a general one. Hence, its usage (in education) is limited to some Scientific Computing and Computational Mathematics courses as a "friendly" programming language for mathematicians who may not have the foundational skills to write good code to do linalg operations from scratch but can translate formulas into expressions (take mat-vec $Ax$ which is just $\verb|A*x|$ in MATLAB).

(I know that MATLAB has an extensive list of toolboxes and favoured by engineers to do, for example, inverse kinematics. But that is beside the argument of this answer)

Lastly, the choice (for scientific computing) would be between C/C++ and Fortran in the past. Nowadays, it is between NumPy/SciPy (row-major) and MATLAB/GNU Octave/SciLab (column-major) for most applications and development (excluding high-performance/parallel computing). You can do pretty much what you want with similar simplicity using either layout, it just comes to preference.

See en.wikipedia.org/wiki/Row-_and_column-major_order for more discussion.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.