there is an error in describing the loop, please help me to resolve this one.

1 回表示 (過去 30 日間)
SAHIL SAHOO
SAHIL SAHOO 2022 年 8 月 30 日
編集済み: Rik 2022 年 8 月 30 日
clear
x = linspace (-1,1,256);
y = linspace (-1,1,256);
[X,Y] = meshgrid(x,y);
r = sqrt(X.^2 + Y.^2);
z= 0;
w = 100;
E0 = 1;
O = rand(256,256);
E = E0*exp(-1i*(O));
A = (abs(E)).^2 ;
x=linspace(-1,1,256);
[X,Y]=meshgrid(x,x);
r=sqrt(X.^2+Y.^2);
R= 0.06;
t = 0.5;
l= 0:36:360;
M = cell(1,length(l));
for i = 1:length(l)
h= t*cosd(l(i));k= t*sind(l(i));
p=sqrt((X-h).^2+(Y-k).^2);
M{i}=(p<R);
end
M = M{1}+M{2}+M{3}+M{4}+M{5}+M{6}+M{7}+M{8}+M{9}+M{10};
Io=1.0;
Y=linspace(1,-1,256); Z=Y ;
A(1:length(Y),1:length(Z))=0;
for i=1:length(Y)
for j=1:length(Z)
q=(Y(i).^2+Z(j).^2).^0.5;
w = 0.3;
A(i,j)=Io.*exp(-((q.^2)./w^2));
end
end
G0 = 15;
I = 1;
E = E0*exp(-1i*(O));
for i = 1:100
G(i) = G0/(1+ (abs(E(i)))^2/I);
E(i+1) = M *(ifft(A*(fft(G* E(i)))));
end
Unable to perform assignment because the left and right sides have a different number of elements.
imagesc(E)
  3 件のコメント
Rik
Rik 2022 年 8 月 30 日
So the question is: what do you want to do?
Answering that question is very hard because you chose non-descriptive variable names and didn't write any comments. How do you expect anyone to understand what is going on in your code? Remember that you in three months time are a different person from current you. How is future you suposed to understand what you did and extend functionality or fix a bug?
SAHIL SAHOO
SAHIL SAHOO 2022 年 8 月 30 日
@Karim please check this one.
clear
x = linspace (-1,1,256);
y = linspace (-1,1,256);
[X,Y] = meshgrid(x,y);
r = sqrt(X.^2 + Y.^2);
z= 0;
w = 100;
E0 = 1;
O = rand(256,256);
E = E0*exp(-1i*(O));
A = (abs(E)).^2 ;
x=linspace(-1,1,256);
[X,Y]=meshgrid(x,x);
r=sqrt(X.^2+Y.^2);
R= 0.06;
t = 0.5;
l= 0:36:360;
M = cell(1,length(l));
for i = 1:length(l)
h= t*cosd(l(i));k= t*sind(l(i));
p=sqrt((X-h).^2+(Y-k).^2);
M{i}=(p<R);
end
M = M{1}+M{2}+M{3}+M{4}+M{5}+M{6}+M{7}+M{8}+M{9}+M{10};
Io=1.0;
Y=linspace(1,-1,256); Z=Y ;
A(1:length(Y),1:length(Z))=0;
for i=1:length(Y)
for j=1:length(Z)
q=(Y(i).^2+Z(j).^2).^0.5;
w = 0.3;
A(i,j)=Io.*exp(-((q.^2)./w^2));
end
end
% i want to apply loop here by using numerate or any for loop but i didn't how i can do
% that.
G0 = 15;
I = 1;
E1 = E0*exp(-1i*(O));
G1 = G0./(1+ (abs(E1))^2/I);
E2 = M *(ifft(A*(fft(G1* E1))));
G2 = G0./(1+ (abs(E2))^2/I);
E3 = M *(ifft(A*(fft(G2* E2))));
G3 = G0./(1+ (abs(E3))^2/I);
E4 = M *(ifft(A*(fft(G3* E3))));
G4 = G0./(1+ (abs(E4))^2/I);
E5 = M *(ifft(A*(fft(G4* E4))));
G5 = G0./(1+ (abs(E5))^2/I);
E6 = M *(ifft(A*(fft(G5* E5))));
G6 = G0./(1+ (abs(E6))^2/I);
E7 = M *(ifft(A*(fft(G6* E6))));
G7 = G0./(1+ (abs(E7))^2/I);
E8 = M *(ifft(A*(fft(G7* E7))));
G8 = G0./(1+ (abs(E8))^2/I);
E9 = M *(ifft(A*(fft(G8* E8))));
G9 = G0./(1+ (abs(E9))^2/I);
E10 = M *(ifft(A*(fft(G9* E9))));
G10 = G0./(1+ (abs(E10))^2/I);
E = M *(ifft(A*(fft(G10* E10))));
imagesc((abs(E)).^2);colorbar

サインインしてコメントする。

採用された回答

Rik
Rik 2022 年 8 月 30 日
編集済み: Rik 2022 年 8 月 30 日
If you're not planning to do anything with the intermediate values, why would you store them?
init % Runs the first part of your code.
G0 = 15;
I = 1;
E = E0*exp(-1i*(O));
for iteration=1:10
G = G0./(1+ (abs(E))^2/I);
E = M *(ifft(A*(fft(G* E))));
end
imagesc((abs(E)).^2);colorbar
If you want to store the intermediate numbers, that is also possible: use cell vectors. That will you give more intutive control. Just remember that Matlab uses 1-indexing, not 0-indexing.
G0 = 15;
I = 1;
E = { E0*exp(-1i*(O)) };
G = { G0./(1+ (abs(E{1}))^2/I) };
for ind=2:10
E{ind} = M *(ifft(A*(fft(G{ind-1}* E{ind-1}))));
G{ind} = G0./(1+ (abs(E{ind}))^2/I);
end
E_final = M *(ifft(A*(fft(G{10}* E{10}))));
imagesc((abs(E_final)).^2);colorbar

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by