%
% Dense matrices can be physically stored on computers memory
% row-wise or column-wise. It is important to take this into account
% when performing matrix computations. It is always much efficient to 
% access sequentially those matrix elements that are also physically stored
% sequentially.  Matlab stores dense matrices by columns.
%
% Next we demonstate different ways to perform operation y=A*x
% (Run several times for more reliable timing)
format compact
clear all
n=2000;
A=rand(n);
x=rand(n,1);

y1=zeros(n,1);
y2=zeros(n,1);

tic
for i=1:n
    y1(i)=A(i,:)*x;    % access by rows
end
toc


tic
for j=1:n
   y2 = y2 + x(j)*A(:,j);    % access by columns
end
toc

tic
y3=A*x;     % Matlab's internal routine
toc

%
% Check for goofs :-)
%
norm(y1-y2), norm(y1-y3)

