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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With