Now, for each of the for-loop examples on the next page, write two programs that generate the same arrays (1-D, 2-D and 3-D), with the following changes:
1. Modify the programs to pre-allocate the arrays first
2. Use vectorisation functions to implement the programs without the use of any loops. You may want to
look at ndgrid, and remember to use array versions of your operators!
% 1-D array (3 * i)
n = 10;
for i=1:n
A(i) = 3*i;
end
% 2-D array where
% B(i,j) = i/j
n = 10;
for i=1:n
for j=1:n
B(i,j) = i/j;
end
end
% 3-D array where
% C(i,j,k) = i*j+k
n = 10;
for i=1:n
for j=1:n
for k=1:n
C(i,j,k) = i*j+k;
end
end
end