lab8solutions

# lab8solutions - carried out by the repitition loop...

This preview shows pages 1–2. Sign up to view the full content.

ECE220 LAB 8 Solutions Made by Rong Guo Edited by Remya K The multiplication of two N-by-N matrices A and B is a N-by-N matrix C. MATLAB has its built-in function for calculating the multiplication of matrix. We use it as our method A. From the definition of the multiplication of matrices, every element C(i,j) in matrix C is multiplication of the i th row in A with the j th column of B. The equation is: ( , ) ( ,1) * (1 , ) (,2) * (2 , ) ( ,3) * (3 ,) .. . ( , ) *( ,) Ci j A i Bj A i Bj A i B j Ai N BNj =++ ++ We will use this equation to program method B. We have to choose appropriate repetition time so that the time difference between the two methods can be clearly observed. We use the repeat_n_times variable to decide this. The problem can be repeated for matrices of different dimensions by changing the value of N. MATLAB Scripts: clear all N=100; % Define the dimension of matrix t(1)=0; % Define the initial value of the variable t. repeat_n_times = 2 ; % The number of times the multiplication has to be

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: carried out by the repitition loop A=randint(N,N,[1,9]); % Generate the first N-by-N matrix containing random integers between 1 and 9. B=randint(N,N,[1,9]); % Generate the second N-by-N matrix containing random integers between 1 and 9. pause % Matrix multiplication method A: using the built-in matrix multiplication operator. tic % saves the current time for count=1:repeat_n_times % repeat n times in the repitition loop C1=A*B; end toc % Measures the elapsed time C1 % Print out C1 disp( 'method A finished' ) pause %Matrix multiplication method B: using the 'repetition loop' %******************************************** tic % saves the current time for count=1:repeat_n_times % repeat n times in the repitition loop for i=1:N for j=1:N for k=1:N t(k+1)=t(k)+A(i,k)*B(k,j); end C2(i,j)=t(N+1); end end end toc % Measures the elapsed time %******************************************** C2 % Print out C2 disp( 'method B finished' )...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

lab8solutions - carried out by the repitition loop...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online