I am working on some Matlab homework and I was having issues conceptualizing the way that it addresses matrices. In Matlab the matrix is address in d(row,col) format.
I have been programming for a while and have always tended to think of a one dimensional array as a horizontal structure with a second dimension extending out from below.
Which of these is a “more correct” way of thinking about an array data structure from the computer’s point of view
Good question +1.
Purely from a Matlab programming perspective, it is best to think of a matrix as a sequence of column vectors. Why? Because this is how Matlab allocates them to your computers memory. That is, two sequential elements in any given column of a matrix will be allocated next to each other in memory. This is sometimes referred to as “column-major order”, and is used in languages such as Fortran, R, and Julia. The opposite is, unsurprisingly, called “row-major order”, and is used in C and Python.
The implication of this is that Matlab will be much faster at performing operations on the columns of a matrix than on the rows. @angainor provided a great answer to a question of mine a few months ago that demonstrates this fact. Based on @angainor’s insight, here is a useful speed test to run:
On my machine, the outcome of the test is:
In other words, operations performed on columns are almost 5 times faster than operations performed on rows!
From a mathematical perspective I don’t trust myself to give a good answer. You could probably get some great insights from math.stackexchange.