How I can separate my code and let it run in parallel, because for loop is making the code execution very slowly

1 次查看(过去 30 天)
clc;
clear;
M=60;
N=(linspace(6,10,5)).^2; %Number of Elements IRS
K=linspace(4,10,7); %Number of Single Antennas UE
L = 3; %Number of paths
fc = 28e9; %Carrier freq
SNR = -10; %Signal to Noise Ratio
Pt = 1; %Total Power
Pn = Pt/(10^(SNR/10)) ; %Noise Power
C_T = zeros(5,7);
Max_iter = 1000;
for iter = 1:Max_iter
for n = N
%% Millimeter wave Channel
G = M_Wave_channel_Matrix(fc, M, n, L, 'BS', 'IRS'); %NxM
%% Diagonal Matrix for the response of the RIS elements
x = rand(1,n);
theta = (2*pi)*x; %Reflecting Angle IRS
Phi = diag(exp(1i*theta)); %phase shift Matrix for IRS, % Set Amp = 1
%% channel IRS-UE
for k = K
F=zeros(k,n);
H=zeros(k,M);
for i=1:k
fk = M_Wave_channel_Matrix(fc, n, 1, L, 'IRS', 'UE'); %1xN
F(i,:)=fk; %KxN
%% channel BS-UE
hk = M_Wave_channel_Matrix(fc, M, 1, L, 'BS', 'UE'); %1xM
H(i,:)=hk; %KxN
end
HT = H+F*Phi*G; %Channel Matrix
W = pinv(HT); %Precoding Matrix
W_bar = W./vecnorm(W,2,1); %Normalized
D_Matrix = HT*W_bar; %Diagonal Matrix
D_Square = (abs(diag(D_Matrix))').^2; % Channel gains
R = D_Square/Pn;
C = log2(1+R); %Capacity
C_T(N==n,k==K) = C_T(N==n,k==K) + 1/Max_iter*sum(C); %Total Capacity
end
end
end

回答(1 个)

Sulaymon Eshkabilov
If you're planning to embed parallel for loop then the general syntax is:
parfor i=1:N
V1_new = ...
V2_new = ...
...
VAR1=V1_new;
VAR2=V2_new;
...
end
In your code, this for loop has to be corrected or removed.
%% channel IRS-UE
for k = K % This err has to be fixed or removed.
...
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by