After reading up on MATLAB vectorization last week, I wrote this code to normalize each row of a N by 3 matrix. The speedup is not enourmous, but it is significant. I tried timming things this weekend on my computer at home, but MATLAB on a 1GHz G4 with 512MB of RAM is not what you would call ‘peppy’. I’ll try on a modeling workstation at the office today and see what’s what when we try a big matrix and it can stay all in memory.

function [ ] = unit_vector_rows_for( N )

%UNTITLED1 Summary of this function goes here

% Detailed explanation goes here

%make a 1×3 matrix, we will consider rows as 3-tuples

a = [1:3] ;

%repeat a so that it becomes a N by 3 matrix, so we can have a big one to

%test

a=repmat(a,N,1);

tic

for i=1:size(a,1)

a(i,:) = a(i,:) ./ norm(a(i,:)) ;

end

k=toc;

fprintf(‘For loop time is %f\n’,k);

function [ ] = unit_vector_rows_mat( N )

%UNTITLED1 Summary of this function goes here

% Detailed explanation goes here

a=[];

%make a 1×3 matrix, we will consider rows as 3-tuples

a = [1:3] ;

%repeat a so that it becomes a N by 3 matrix, so we can have a big one to

%test

a=repmat(a,N,1);

tic

a = a./repmat(sqrt(sum((a.^2)’))’,1,3);

k = toc;

fprintf(‘Vectorized time is %f\n’,k);