Why when I run this code I got this error messege "Index exceeds array bounds." and this "Matrix dimensions must agree." ?

2 次查看(过去 30 天)
Why, when I run this code I got result of " SINR_Dcell " BUT i get the error messege this time "Index exceeds array bounds." and another run I get this error messege "Matrix dimensions must agree." ? please have a look at my code
maxrange=200;
P_gNB=46;
P_gNB = 10.^((P_gNB-30)/10); % ratio value or in watt
NumDrone= 2; % Number of drones base stations
NumUEs=5; %Number of users
center=[0 0]; % Center of circle for the network layout
tI=1; %[sec] , minimum turning update interval, we will assume
t=1;
BW=20e6;
bw=BW/NumUEs;
N_therDencity = -80;%[dbm]-101;
Nf=9;
N_power = (10.^(N_therDencity./10)./1000).*BW*Nf; %dB thermal noise power for each RB
%N_power = -174 + 10*log10(BW) + Nf; %Compute noise power
P_Drone = 30;% [dbm]
P_Drone = 10.^((P_Drone-30)/10); % in watt other way P_Drone = 10 ^ (P_Drone_dBm / 10);
%P_Drone = db2pow(30);
%Pue= PgNB./NumUEs; % power of each user
NumgNB=1;
h_uav=100;
h=30; %And UAV-UE and h^2 is the height of UAV-BS.
R=25; % raduis of each UAV-BS cell
fc=10e9;
c=3e8;
ro=1000;
h_ue = 1.5;
delta_h = h_uav - h_ue;
m=1;
alpha=3;
T=10; % Simulation Time
time = 0 : tI : T;
tLen = length(time);
%Realizations=5;
SINR_DU = zeros(tLen); % here I want to find sinr during all realization and time lenth
SINR_BU = zeros(tLen);
SINR_BD =zeros(tLen);
k=1;
z=1;
kk=1;
tic
for ti=0:z:tLen
PosgNB_x = [0 0, 0 0].';
PosgNB_y = [0 0, 0 0].';
PosgNB = [PosgNB_x,PosgNB_y];%fixed ground node (fixed ground base station)
theta_Drone=2*pi*(rand(NumDrone,1)); % distributed random number of Base stations
g = 0.5 * ro + 0.5 * ro * rand(NumDrone,1); % let the drones deployed away from the center of circle network layout
PosDrone_x=center(1)+g.*cos(theta_Drone); % Initial positions
PosDrone_y=center(2)+g.*sin(theta_Drone);
PosDrone = [PosDrone_x ,PosDrone_y]; % Air nodes (Drone Base stations)
theta1 = rand(NumUEs, 1) * 2*pi; % distributed random number of Users
r1 = ro * sqrt(rand(NumUEs, 1));
PosUE_x = r1 .* cos(theta1(:)) + center(:,1); %[r1 .* cos(theta1(:)) + center(:,1)];
PosUE_y = r1 .* sin(theta1(:)) + center(:,2); %[r1 .* sin(theta1(:)) + center(:,2)];
PosUE = [PosUE_x,PosUE_y]; % fixed ground users (fixed ground nodes it sould be served or associated to the fixed ground node and Air nodes (drones))
PosUAV3D = [PosDrone, h_uav * ones(NumDrone,1)]; % 3D distance between drones and fixed ground users
dist_D_UE = (PosUE(:,1)-PosUAV3D(:,1).').^2 + (PosUE(:,2)-PosUAV3D(:,2).').^2; % drones to UE distances
dist_gNB_UE = (PosUE(:,1)-PosgNB_x.').^2 + (PosUE(:,2)-PosgNB_x.').^2; % distance between fixed ground Base stations and ground users
dist_gNB_D = hypot(PosDrone_x-PosgNB_x(1,:).', PosDrone_y-PosgNB_y(1,:).'); % distance between fixed ground Base station and drones (air nodes)
[mindistD, assigned_BS] = min(dist_D_UE,[],2); % associate the ground users by nearest air nodes (drones)
[mindistgNB, assigned_BS1] = min(dist_gNB_UE,[],2); % associate the ground users by nearest fixed ground base station
[mindistgNBD, assigned_BD] = min(dist_gNB_D, [],1); % % associate the air nodes (drones) by nearest fixed ground base station
inrange = mindistD <= maxrange^2; % range of drone coverage, which is supposed to serve the ground users inside this range
inrangegNB = ~inrange; % range of fixed ground base station it is supposed to serve the fixed ground users which located outside the range of drone
r_B0D0 =assigned_BD; % distance between the serving fixed ground base station and the air nodes (drones)
r_BxU = assigned_BS1(inrange); % distance between interfering fixed ground base station and the users that assumed to be served by drones
r_B0U = assigned_BS1; % distance between serving fixed ground base station and fixed ground user that assumed to be serve by fixed ground base station
r_D0U = assigned_BS; % distance between the serving drone and fixed ground user that assumed to be serve by drones
r_DxU = assigned_BS(inrangegNB); % distance between interfering Drone and user that assumed to be served by drones
Fading_B2U = gamrnd(m, 1 / m, NumgNB, 1); % fading between fixed ground base station ground users
Fading_D2U = gamrnd(m, 1 / m,NumUEs,1); % fading between drone and ground users
Fading_D2U0 = Fading_D2U(assigned_BS); % fading between drone and its ground users
Fading_B2D = gamrnd(m, 1 / m, NumgNB, 1); % fading beteen fixed ground base station and its drones
P_B0_Rx = P_gNB .* Fading_B2U(assigned_BS1) .* r_B0U .^ (-alpha); %Power recieved from serving gNB to its ground users
P_D0_Rx = P_Drone .* Fading_D2U0 .* r_D0U .^ (-alpha); %power recieved from serving Drone to its ground users
P_B0D0_Rx = P_gNB .* Fading_B2D .* r_B0D0 .^ (-alpha); % power recieved from serving gNB to its drones
I_BU = sum(P_gNB .* Fading_B2U .* r_BxU .^(-alpha)) - P_B0_Rx; % interfernce that caused by fixed ground base station
I_DU = sum(P_Drone .* Fading_D2U .* r_DxU .^(-alpha)) - P_D0_Rx; % interfernce that caused by drones
I_U = I_BU + I_DU; % total interference
SINR_BU = P_B0_Rx / (I_DU + P_D0_Rx + N_power); % SINR for users that served by fixed ground Base station
SINR_BD = P_B0D0_Rx / N_power; % In our assumption there is no interfered fixed ground base station due to only one that should serve the drones so this is SNR
SINR_DU = P_D0_Rx ./ (I_U + P_B0_Rx + N_power); % SINR for users that served by drones
Sum_SINR_DULiner = sum(SINR_DU); % I summed it due to apeared as vector (I dont know if its correct or not)
Sum_SINR_DU = 10*log10(Sum_SINR_DULiner); % I converet SINR from linear to dB
SINR_Dcell = zeros(tLen); % SINR_Dcell is the SINR for the whole coverage area of drone
for jj=1:1:assigned_BS
SINR_Dcell = sum(Sum_SINR_DU) % SINR_Dcell(jj,:) = sum(SINR_DU); can be used
end
newSINR_Dcell = SINR_Dcell(ti+1);
end
  6 个评论
Jan
Jan 2022-3-8
I'm lost, because I do not know a method to produce two different errors. Matlab stops after the first error, so how can a seconds one appear?!
omar th
omar th 2022-3-8
the second error appeared when re-run the code (when I repeat the execution). Anyway I fixed the first error, please try with this code you will see the second error.
maxrange=200;
P_gNB=46;
P_gNB = 10.^((P_gNB-30)/10); % ratio value or in watt
NumDrone= 2; % Number of drones base stations
NumUEs=5; %Number of users
center=[0 0]; % Center of circle for the network layout
tI=1; %[sec] , minimum turning update interval, we will assume
t=1;
BW=20e6;
bw=BW/NumUEs;
N_therDencity = -80;%[dbm]-101;
Nf=9;
N_power = (10.^(N_therDencity./10)./1000).*BW*Nf; %dB thermal noise power for each RB
%N_power = -174 + 10*log10(BW) + Nf; %Compute noise power
P_Drone = 30;% [dbm]
P_Drone = 10.^((P_Drone-30)/10); % in watt other way P_Drone = 10 ^ (P_Drone_dBm / 10);
%P_Drone = db2pow(30);
%Pue= PgNB./NumUEs; % power of each user
NumgNB=1;
h_uav=100;
h=30; %And UAV-UE and h^2 is the height of UAV-BS.
R=25; % raduis of each UAV-BS cell
fc=10e9;
c=3e8;
ro=1000;
h_ue = 1.5;
delta_h = h_uav - h_ue;
m=1;
alpha=3;
T=10; % Simulation Time
time = 0 : tI : T;
tLen = length(time);
%Realizations=5;
SINR_DU = zeros(tLen); % here I want to find sinr during all realization and time lenth
SINR_BU = zeros(tLen);
SINR_BD =zeros(tLen);
k=1;
z=1;
kk=1;
tic
for ti=0:z:tLen
PosgNB_x = [0 0, 0 0].';
PosgNB_y = [0 0, 0 0].';
PosgNB = [PosgNB_x,PosgNB_y];%fixed ground node (fixed ground base station)
theta_Drone=2*pi*(rand(NumDrone,1)); % distributed random number of Base stations
g = 0.5 * ro + 0.5 * ro * rand(NumDrone,1); % let the drones deployed away from the center of circle network layout
PosDrone_x=center(1)+g.*cos(theta_Drone); % Initial positions
PosDrone_y=center(2)+g.*sin(theta_Drone);
PosDrone = [PosDrone_x ,PosDrone_y]; % Air nodes (Drone Base stations)
theta1 = rand(NumUEs, 1) * 2*pi; % distributed random number of Users
r1 = ro * sqrt(rand(NumUEs, 1));
PosUE_x = r1 .* cos(theta1(:)) + center(:,1); %[r1 .* cos(theta1(:)) + center(:,1)];
PosUE_y = r1 .* sin(theta1(:)) + center(:,2); %[r1 .* sin(theta1(:)) + center(:,2)];
PosUE = [PosUE_x,PosUE_y]; % fixed ground users (fixed ground nodes it sould be served or associated to the fixed ground node and Air nodes (drones))
PosUAV3D = [PosDrone, h_uav * ones(NumDrone,1)]; % 3D distance between drones and fixed ground users
dist_D_UE = (PosUE(:,1)-PosUAV3D(:,1).').^2 + (PosUE(:,2)-PosUAV3D(:,2).').^2; % drones to UE distances
dist_gNB_UE = (PosUE(:,1)-PosgNB_x.').^2 + (PosUE(:,2)-PosgNB_x.').^2; % distance between fixed ground Base stations and ground users
dist_gNB_D = hypot(PosDrone_x-PosgNB_x(1,:).', PosDrone_y-PosgNB_y(1,:).'); % distance between fixed ground Base station and drones (air nodes)
[mindistD, assigned_BS] = min(dist_D_UE,[],2); % associate the ground users by nearest air nodes (drones)
[mindistgNB, assigned_BS1] = min(dist_gNB_UE,[],2); % associate the ground users by nearest fixed ground base station
[mindistgNBD, assigned_BD] = min(dist_gNB_D, [],1); % % associate the air nodes (drones) by nearest fixed ground base station
inrange = mindistD <= maxrange^2; % range of drone coverage, which is supposed to serve the ground users inside this range
inrangegNB = ~inrange; % range of fixed ground base station it is supposed to serve the fixed ground users which located outside the range of drone
r_B0D0 =assigned_BD; % distance between the serving fixed ground base station and the air nodes (drones)
r_BxU = assigned_BS1(inrange); % distance between interfering fixed ground base station and the users that assumed to be served by drones
r_B0U = assigned_BS1; % distance between serving fixed ground base station and fixed ground user that assumed to be serve by fixed ground base station
r_D0U = assigned_BS; % distance between the serving drone and fixed ground user that assumed to be serve by drones
r_DxU = assigned_BS(inrangegNB); % distance between interfering Drone and user that assumed to be served by drones
Fading_B2U = gamrnd(m, 1 / m, NumgNB, 1); % fading between fixed ground base station ground users
Fading_D2U = gamrnd(m, 1 / m,NumUEs,1); % fading between drone and ground users
Fading_D2U0 = Fading_D2U(assigned_BS); % fading between drone and its ground users
Fading_B2D = gamrnd(m, 1 / m, NumgNB, 1); % fading beteen fixed ground base station and its drones
P_B0_Rx = P_gNB .* Fading_B2U(assigned_BS1) .* r_B0U .^ (-alpha); %Power recieved from serving gNB to its ground users
P_D0_Rx = P_Drone .* Fading_D2U0 .* r_D0U .^ (-alpha); %power recieved from serving Drone to its ground users
P_B0D0_Rx = P_gNB .* Fading_B2D .* r_B0D0 .^ (-alpha); % power recieved from serving gNB to its drones
size(P_Drone)
size(Fading_D2U)
size(r_DxU)
size(alpha)
I_BU = sum(P_gNB .* Fading_B2U .* r_BxU .^(-alpha)) - P_B0_Rx; % interfernce that caused by fixed ground base station
I_DU = sum(P_Drone .* Fading_D2U .* r_DxU .^(-alpha)) - P_D0_Rx; % interfernce that caused by drones
I_U = I_BU + I_DU; % total interference
SINR_BU = P_B0_Rx / (I_DU + P_D0_Rx + N_power); % SINR for users that served by fixed ground Base station
SINR_BD = P_B0D0_Rx / N_power; % In our assumption there is no interfered fixed ground base station due to only one that should serve the drones so this is SNR
SINR_DU = P_D0_Rx ./ (I_U + P_B0_Rx + N_power); % SINR for users that served by drones
Sum_SINR_DULiner = sum(SINR_DU); % I summed it due to apeared as vector (I dont know if its correct or not)
Sum_SINR_DU = 10*log10(Sum_SINR_DULiner); % I converet SINR from linear to dB
SINR_Dcell = zeros(tLen); % SINR_Dcell is the SINR for the whole coverage area of drone
for jj=1:1:assigned_BS
SINR_Dcell(jj) = sum(Sum_SINR_DU) % SINR_Dcell(jj,:) = sum(SINR_DU); can be used
end
%newSINR_Dcell = SINR_Dcell;
end

请先登录,再进行评论。

回答(1 个)

Benjamin Thompson
When multiplying by a scalar, do not use .*. Making this chang allows the first thee iterations of your loop to pass.
I_DU = sum(P_Drone * Fading_D2U .* r_DxU .^(-alpha)) - P_D0_Rx; % interfernce that caused by drones
But on the fourth iteration r_DxU has only four elements compared to Fading_D2U having five so the multiplication fails. Try stepping through your code using breakpoints in the editor to see what is going on.

Community Treasure Hunt

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

Start Hunting!

Translated by