Info

此问题已关闭。 请重新打开它进行编辑或回答。

How to optimize this program using a for loop? i want to repeat the part between percentage symbols n-times, not only 4, i use the E1 to build the E2, and then, E2 to build the E3, and so on, Ifrog is just a matrix of 4 columns and 2000 rows.

1 次查看(过去 30 天)
load Ifrog
dimension = 2000;
x = linspace(-5,5,dimension);
L = length(x);
n = 2^nextpow2(L);
FG1 = exp((-((x-2.5)./(1)).^2));
FG2 = exp((-((-x-2.5)./(1)).^2));
TFG1 = fftshift(FG1,n);
TFG2 = fftshift(FG2,n);
x1=linspace(0,.8,dimension);
w=2*pi./x1;
Fexpo = (1+exp(-2*w));
Convol = conv(TFG1,TFG2,'same');
kernel = 2*(Convol.*Fexpo);
a = led01(1:dimension);
b = led02(1:dimension);
c = led03(1:dimension);
d = led04(1:dimension);
mod1 = [a, a, a, a, a];
mod2 = [b, b, b, b, b];
mod3 = [c, c, c, c, c];
mod4 = [d, d, d, d, d];
traza = [mod1, mod2, mod3, mod4];
%%%%%%%%%%%
%%%%%%%%%%%
%%%%%%%%%%%
for m=1:20;
if m==3|8|13|18
E1(:,m) = ifft(fft(kernel')/norm(fft(kernel)).*traza(:,m));
else
E1(:,m) = traza(:,m);
end
end
for m=1:20;
if m==3|8|13|18
E2(:,m) = ifft(fft(E1(:,m))/norm(fft(E1(:,m))).*traza(:,m));
else
E2(:,m) = ifft(fft(E1(:,m))/norm(fft(E1(:,m))));
end
end
for m=1:20;
if m==3|8|13|18
E3(:,m) = ifft(fft(E2(:,m))/norm(fft(E2(:,m))).*traza(:,m));
else
E3(:,m) = ifft(fft(E2(:,m))/norm(fft(E2(:,m))));
end
end
for m=1:20;
if m==3|8|13|18
E4(:,m) = ifft(fft(E3(:,m))/norm(fft(E3(:,m))).*traza(:,m));
else
E4(:,m) = ifft(fft(E3(:,m))/norm(fft(E3(:,m))));
end
end
%%%%%%%%%%%
%%%%%%%%%%%
%%%%%%%%%%%
sumren = sum(abs(E4),1);
sumcol = sum(abs(E4),2);
figure()
subplot(2,2,1)
surf(abs(E4))
subplot(2,2,2)
contour(abs(E4))
subplot(2,2,3)
plot(abs(sumren))
subplot(2,2,4)
plot(abs(sumcol))

回答(1 个)

Walter Roberson
Walter Roberson 2016-9-11
Use cell arrays.
E = cell{n, 1};
for m=1:20;
if ismember(m, [3, 8, 13, 18])
E{1}(:,m) = ifft(fft(kernel')/norm(fft(kernel)).*traza(:,m));
else
E{1}(:,m) = traza(:,m);
end
end
for idx = 2 : n
for m=1:20;
if ismember(m, [3, 8, 13, 18])
E{idx}(:,m) = ifft(fft(E{idx-1}(:,m))/norm(fft(E{idx-1}(:,m))).*traza(:,m));
else
E{idx}(:,m) = ifft(fft(E{idx-1}(:,m))/norm(fft(E{idx-1}(:,m))));
end
end
Please take note of the correction for your test on m
  2 个评论
Stephen23
Stephen23 2016-9-11
编辑:Stephen23 2016-9-11
@David Zarate Villegas: and these things are so secret that you are not going to tell us so that we can help you to fix them.
Walter Roberson gave you a very good solution: using cell arrays is a very neat and robust way to solve your problem. We are also happy to help you fix any bugs making it work properly. However when you write "i got things" this does not really tell us anything useful: do you get error messages, warnings, or unexpected values ? How are you checking them, and what code are you using?
When you actually tell us information so that we can replicate your results and you tell us what you want the code to be be doing then we can help you much more easily. At the moment you are expecting us rely on our magical glass balls and predict what is shown on your computer screen...

此问题已关闭。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by