% % Written by: % -- % John L. Weatherwax 2006-09-03 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- clc; n = 1200; for jay = 1:4 if( jay > 1 ) oldtime = time; end matrixsize = n A = randn(n); x = randn(n,1); % First multiplication method (matlab's notation): % t1 = cputime; b = A*x; time1(jay) = cputime - t1; % Second multiplication method row-oriented matrix-vector with a for loop: % t2 = cputime; b=zeros(n,1); for ii=1:n, b(ii)=0; for jj=1:n b(ii) = b(ii)+A(ii,jj)*x(jj); end end time2(jay) = cputime - t2; % Third multiplication method column-oriented matrix-vector with a for loop: % t3 = cputime; b=zeros(n,1); for jj=1:n, for ii=1:n b(ii) = b(ii)+A(ii,jj)*x(jj); end end time3(jay) = cputime - t3; if( jay > 1 ) ratio = time/oldtime end n = 2*n; end time1 time2 time3