Solve
Use n=150:
n onescript, copy-and-paste the nested for-loop 3-D array program as given below.
% 3-D array where
% C(i,j,k) = i*j+k
tic
n = 150;
for i=1:n
for j=1:n
for k=1:n
C(i,j,k) =i*j+k;
end
end
end
toc
Elapsed time is 5.827000 seconds.
In thesecond script, add the array pre-allocation version of the program
tic
n = 150;
C=zeros(n,n,n);
for i=1:n
for j=1:n
for k=1:n
C(i,j,k) =i*j+k;
end
end
end
toc
Elapsed time is 0.430000 seconds.
In thethird script, insert your vectorised version of the program:
tic
n = 150;
C=zeros(n,n,n);
k=linspace(1,1,n);
IJ=(k')*k;
for i=1:n
C(:,:,i)=IJ+i;
end
toc
Comments
Leave a comment