i want to ask how can i store the value of d_eb for each iteration of loop

1 回表示 (過去 30 日間)
Maimoona Asad
Maimoona Asad 2021 年 12 月 16 日
コメント済み: Maimoona Asad 2021 年 12 月 16 日
I am stuck in one thing if you could help please. how can i store deb mentoined by bold letters in the end for each iteration. code: %% Large intelligent reflecting surfaces X3=[-7.655; -6.189; -3.251; 2.926; 6.534; 10.79; 5.203; -4.465; 1.42; -8.66; -0.8748; 6.534] ; Y3=[1.393; 10.42; 6.639; 5.821; 8.425; 2.924; 1.139; 0.7508; -7.917; -6.928; -10.01; -7.03]; plot(X3,Y3,'r*','MarkerSize', 10, 'Linewidth', 2); %legend('','Base stations','Users','Intelligent Reflecting Surfaces') hold on l=0.03; p=0; %% loop for the elements of irs x_e = zeros(10, 10, 12); y_e = zeros(10, 10, 12);
for i=1:L for j=1:elements for k=1:elements x3(j,k) = X3(i,1) + p; % X coordinate of an element of IRS for plotting y3(j,k) = Y3(i,1); % Y coordinate of an element of IRS for plotting p=k*l; x_e(j,k,i) = x3(j,k); y_e(j,k,i)= y3(j,k); end p=0; Y3(i,1) = Y3(i,1) + l; end plot(x3,y3,'bo') end for s=1:4 for n=1:12 for p=1:10 for r=1:10 d_eb=sqrt(((x_e(p,r,n)-x_b(s,1))^2)+((y_e(p,r,n)-y_b(s,1))^2)); % distance end
end
end
end
thanks

採用された回答

Voss
Voss 2021 年 12 月 16 日
Case 1: If you want one d_eb value for each (x_e, y_e) point, you can initialize d_eb to be the same size as x_e and store each element of d_eb as it is calculated:
x_e = zeros(10, 10, 12);
y_e = zeros(10, 10, 12);
d_eb = zeros(10, 10, 12);
for s=4
for i=1:L
for j=1:elements
for k=1:elements
x3(j,k) = X3(i,1) + p; % X coordinate of an element of IRS for plotting
y3(j,k) = Y3(i,1); % Y coordinate of an element of IRS for plotting
p=k*l;
x_e(j,k,i) = x3(j,k);
y_e(j,k,i)= y3(j,k);
end
p=0;
Y3(i,1) = Y3(i,1) + l;
end
plot(x3,y3,'bo')
end
for n=1:12
for p=1:10
for r=1:10
d_eb(p,r,n)=sqrt(((x_e(p,r,n)-x_b(s,1))^2)+((y_e(p,r,n)-y_b(s,1))^2)); % distance
end
end
end
end
Case 2: If you want to keep d_eb values for each value of s as well (currently s is always only 4), you can initialize d_eb to be a 4-D matrix with the fourth dimension representing the values for each value of s:
x_e = zeros(10, 10, 12);
y_e = zeros(10, 10, 12);
s_all = 4;
NS = numel(s_all);
d_eb = zeros(10, 10, 12, NS);
for s=1:NS
for i=1:L
for j=1:elements
for k=1:elements
x3(j,k) = X3(i,1) + p; % X coordinate of an element of IRS for plotting
y3(j,k) = Y3(i,1); % Y coordinate of an element of IRS for plotting
p=k*l;
x_e(j,k,i) = x3(j,k);
y_e(j,k,i)= y3(j,k);
end
p=0;
Y3(i,1) = Y3(i,1) + l;
end
plot(x3,y3,'bo')
end
for n=1:12
for p=1:10
for r=1:10
d_eb(p,r,n,s)=sqrt(((x_e(p,r,n)-x_b(s_all(s),1))^2)+((y_e(p,r,n)-y_b(s_all(s),1))^2)); % distance
end
end
end
end
Note that in this case I redefined s to be an index into a new vector called s_all, which contains all values of s to be used, and I modified the indexing into x_b and y_b accordingly.
In either case, you can avoid the last triple for loop over n, p, r by using element-wise operations (.^ instead of ^) in your distance calculation. For example, in Case 1, this:
for n=1:12
for p=1:10
for r=1:10
d_eb(p,r,n)=sqrt(((x_e(p,r,n)-x_b(s,1))^2)+((y_e(p,r,n)-y_b(s,1))^2)); % distance
end
end
end
Is the same as this:
d_eb = sqrt(((x_e-x_b(s,1)).^2)+((y_e-y_b(s,1)).^2)); % distance
And in Case 2, this:
for n=1:12
for p=1:10
for r=1:10
d_eb(p,r,n,s)=sqrt(((x_e(p,r,n)-x_b(s_all(s),1))^2)+((y_e(p,r,n)-y_b(s_all(s),1))^2)); % distance
end
end
end
is the same as this:
d_eb(:,:,:,s) = sqrt(((x_e-x_b(s_all(s),1)).^2)+((y_e-y_b(s_all(s),1)).^2)); % distance

その他の回答 (1 件)

Torsten
Torsten 2021 年 12 月 16 日
d_eb = sqrt((x_e - x_b(4,1)).^2 + (y_e - y_b(4,1)).^2); % distance
without any loops.
  1 件のコメント
Maimoona Asad
Maimoona Asad 2021 年 12 月 16 日
I want to do it with loop something like this D_deb(p,r,n,s)

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by