You can vectorize this easily for huge computational savings:
tic
E0=10;
A = zeros(10000,10000);
for k = 1:10000
for j = 1:10000
A(k,j)=E0*exp(-10^(-6.5)*((k-5000)^2+(j-5000)^2));
end
end
for k=1:10:9990
for j=1:10:9990
X=rand*pi;
for h=1:10
for l=1:10
A(k+l-1,j+h-1) = A(k+l-1,j+h-1) *exp(+1i*X);
end
end
end
end
disp('Using loops:')
toc
tic
E0=10;
N = 10000;
m = 10;
nX = N/m;
XX = pi*rand(nX);
x = ((1:N).'-N/2).^2;
A = E0*exp(-10^(-6.5)*(x+x.'));
krX = kron(exp(1i*XX),ones(m));
A = A.*krX;
disp('Vectorizing:')
toc
which yields output on my machine:
Using loops:
Elapsed time is 11.358504 seconds.
Vectorizing:
Elapsed time is 0.577795 seconds.
By the way, I did the vectorization assuming that using 9990 as the upper bounds for k and j in your second loop structure was an error on your part. With how you did it, the bottom-right 10x10 block of A wouldn't be mutiplied by a random number. If you wanted it to be, you should instead use 9991 (or really any number from 9991 up to 10000) as the upper limits. If, on the other hand, how you had it was how you wanted, then immediately after the line where XX is defined in the vectorization code, you should do XX(nX,nX)=0.
0 Comments
Sign in to comment.