Algo
Algo

Reputation: 198

Why matrices are [rows, columns] and not [columns, rows]?

For the sake of simplicity, I will refer to column as col.

Why are matrices defined as [rows, columns] and not [columns, rows]? It has just caused me a ton of headaches and confusions.

My thinking goes this way:

  1. A regular array:

    [1, 3, 5, 2, 4] 
    

    is like a matrix with one row and multiple cols. And it is notated like that: arr[n].

    And so if we had another dimension:

    [1, 3, 5, 2, 4]
    [1, 3, 6, 3, 6]
    

    there are now rows. So let us notate the rows after the 'n', arr[n, rows], but the reality shows us otherwise.

  2. In addition, a matrix of 2 dimensions can be looked at as a cartesian coordinate system (where the direction of the y axis is flipped, the origin is element [0,0]). in the plane, we notate points like that: (X,Y).

It looks like the cols are sitting on the x axis and the rows are on the y axis, so why not notate the elements of matrices like that: [Cols, Rows]?

Sorry if I've confused you, and sorry for my ignorance.

Upvotes: 4

Views: 3079

Answers (1)

aka.nice
aka.nice

Reputation: 9572

IMO, this is bound to latin typographical conventions.

Latin writes from left to right, then top to bottom. Following these conventions, a Matrix is decomposed into nRow rows. Each row is then decomposed into nColum elements, much like you would decompose a text in sentences, and sentences in words.

Information is thus organized with a most significant arrangement (rows) and least significant (column).

Following same convention as latin (err arabic) number notation, we have most significant nRow on the left (first if you are latin), and least significant nColumn on the right, thus (nRow,nColumn) for describing the layout.

Naturally, accessing a single element at row iRow and column jCol follows same convention (iRow,jCol).

Note that information can be arranged completely differently in underlying software. For example, for multi-dimensional arrays in FORTRAN and Matlab, first indice vary first, and the sequence in memory is x(1,1) x(2,1) x(3,1) ... x(1,2) x(2,2) x(3,2) ... sort of column-wise order if we consider that left (first) is row index, right (last) is column index. Or maybe some optimized library will have arranged a block layout for the matrix.

Upvotes: 3

Related Questions