# Why the rows are the first dimension of array by design?

26 views (last 30 days)

Answered: Walter Roberson on 13 Sep 2020
I know it's impossible to change now because of the compatibility burden. But why did the first developers decided to use rows as the first dimension? Isn't it quite weird?
When we type text, we fill all columns before we enter the next row - the first dimension is columns. When an image is written into the disk, the columns of the first row is written before that of the next row - the first dimension is still columns. Even when we code a literal matrix in MATLAB, we start by typing all the columns of the first row:
A=[1 2 3
4 5 6
7 8 9];
Now when we read an image file whose format is not yet supported by imread(), we usually use low-level fread(). However, since image files are usually written row-by-row, we have to transpose the array read, before imshow().
I just can't find a scene where it is more convenient to put rows at the first dimension. I hope if anyone familiar with the history of MATLAB can tell me how did things become like this.

Walter Roberson on 13 Sep 2020
It is because MATLAB was originally created on top of FORTRAN and FORTRAN used that order.
When an image is written into the disk, the columns of the first row is written before that of the next row
That depends on the file format.
There are two major ways of organizing 2D data in common use.
Cartesian geometry has evolved to have the convention of having the independent variable be the horizontal axes, but that was not always the case.
Data like
1 2 3
4 5 6
7 8 9
is read left-to-right top-to-bottom in most Indo-European cultures, but tables are read right-to-left in a number of languages (such as Arabic), and is read top-to-bottom right-to-left in some cultures (Japanese)
Do not make the mistake of assuming that what you are accustomed to is the only possibility or the "best" possibility.
Consider tables of data, such as the log tables shown at http://www.sliderules.info/a-to-z/log-tables.htm . When given a number to look up, do you search along the columns for it? NO, you search down along the rows to find the proper row, and look up the details along the columns.
These are strong but opposed conventions both in very common use. Tables / Charts of data have the major selector going down and details across, but Cartesian plots, and English presentations of arrays have the major selector going across and the details going vertically.
Neither one is wrong: both are fine within their contexts.
People gripe about "Why can't MATLAB be row-major? C is row-major!" .... but if you look carefully at how most C programs store 2D arrays, most of them do not use arrays of consecutive memory, and instead each array dimension except the last is vectors of pointers to lower-level vectors. Multidimensional arrays of consecutive memory is possible in C, but it is not what most programs use.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by