Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should arrays be thought of as horizontal or vertical structures

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

like image 981
secretformula Avatar asked Jan 25 '13 04:01

secretformula


1 Answers

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:

M = 1000; %# Number of iterations over each method
T = 1000; %# Number of rows
N = 1000; %# Number of columns

X = randn(T, N); %# Random matrix

%# Loop over the rows of a matrix and perform a sum operation on each row vector
tic
for m = 1:M
    for t = 1:T
        sum(X(t, :));
    end
end
toc

%# Loop over the columns of a matrix and perform a sum operation on each column vector
tic
for m = 1:M
    for n = 1:N
        sum(X(:, n));
    end
end
toc

On my machine, the outcome of the test is:

Elapsed time is 9.371870 seconds. %# Looping over rows
Elapsed time is 1.943970 seconds. %# Looping over columns

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.

like image 95
Colin T Bowers Avatar answered Oct 06 '22 01:10

Colin T Bowers