Can I sort each of these group associated random points SEPARATELY for each point ?

2 次查看(过去 30 天)
Can I sort these group of assigned point seperatly, which means I want to know exactly the group of RED points (as vectors it is not necessary to plot them) that assigned to blue Pentagon seperately. Thanks in advance
the line that I want to change it
plot([PosUE(inrange,1) PosBSs_x(BSidx(inrange))]', ...
[PosUE(inrange,2) PosBSs_y(BSidx(inrange))]','b');
whole code
ro=1000; % radius of the layout circle
NumBSs=6; % Number of Base stations
NumUEs=50; % Number of users
center=[0 0]; % center of the circle
maxrange = 250; % max range for base stations (stars)
theta_BSs=2*pi*(rand(NumBSs,1)); % distributed random number of Base stations
g = 0.5 * ro + 0.5 * ro * rand(NumBSs,1);
PosBSs_x=center(1)+g.*cos(theta_BSs);
PosBSs_y=center(2)+g.*sin(theta_BSs);
theta1 = rand(NumUEs, 1) * 2*pi; % distributed random number of Users
r1 = ro * sqrt(rand(NumUEs, 1));
PosUE = [r1 .* cos(theta1(:)) + center(1),r1 .* sin(theta1(:)) + center(2)];
% find which UE points are within range of a BS
D = (PosUE(:,1)-PosBSs_x.').^2 + (PosUE(:,2)-PosBSs_y.').^2;
[minD BSidx] = min(D,[],2);
inrange = minD <= maxrange^2;
numinrange = nnz(inrange);
% Initial plot objects
hfig = figure(2);
hax=axes('parent',hfig);
% Plot of deploying points
hold on
plot([PosUE(inrange,1) PosBSs_x(BSidx(inrange))]', ...
[PosUE(inrange,2) PosBSs_y(BSidx(inrange))]','b');
plot([PosUE(:,1) repmat(center(1),NumUEs,1)]', ...
[PosUE(:,2) repmat(center(2),NumUEs,1)]','k');
plot(center(1),center(2),'k.','MarkerSize', 20)
plot(PosBSs_x,PosBSs_y,'bp','MarkerSize', 12)
plot(PosUE(inrange,1),PosUE(inrange,2),'r.','MarkerSize', 12);
plot(PosUE(~inrange,1),PosUE(~inrange,2),'r.','MarkerSize', 12);
% Plot the layout as circles
t = linspace(0, 2*pi);
plot(ro * cos(t) + center(1),ro * sin(t) + center(2))
grid on
hold(hax, 'on')
axis(hax, 'equal')

回答(1 个)

Divyam
Divyam 2024-8-30
To sort the users assigned to each base station separately you need to find the closest base station for each user and store the data for the base stations accordingly with the data of user location.
Here is the code for your reference:
ro = 1000; % radius of the layout circle
NumBSs = 6; % Number of Base stations
NumUEs = 50; % Number of users
center = [0 0]; % center of the circle
maxrange = 250; % max range for base stations (stars)
theta_BSs = 2 * pi * (rand(NumBSs, 1)); % distributed random number of Base stations
g = 0.5 * ro + 0.5 * ro * rand(NumBSs, 1);
PosBSs_x = center(1) + g .* cos(theta_BSs);
PosBSs_y = center(2) + g .* sin(theta_BSs);
theta1 = rand(NumUEs, 1) * 2 * pi; % distributed random number of Users
r1 = ro * sqrt(rand(NumUEs, 1));
PosUE = [r1 .* cos(theta1(:)) + center(1), r1 .* sin(theta1(:)) + center(2)];
% Find which Users are within range of a Base Station
D = (PosUE(:,1) - PosBSs_x.').^2 + (PosUE(:,2) - PosBSs_y.').^2;
[minD, BSidx] = min(D, [], 2);
inrange = minD <= maxrange^2;
% Initial plot objects
hfig = figure(2);
hax = axes('parent', hfig);
% Plot of deploying points
hold on
plot([PosUE(inrange,1) PosBSs_x(BSidx(inrange))]', ...
[PosUE(inrange,2) PosBSs_y(BSidx(inrange))]','b');
plot([PosUE(:,1) repmat(center(1), NumUEs, 1)]', ...
[PosUE(:,2) repmat(center(2), NumUEs, 1)]','k');
plot(center(1), center(2), 'k.', 'MarkerSize', 20);
plot(PosBSs_x, PosBSs_y, 'bp', 'MarkerSize', 12);
plot(PosUE(inrange,1), PosUE(inrange,2), 'r.', 'MarkerSize', 12);
plot(PosUE(~inrange,1), PosUE(~inrange,2), 'r.', 'MarkerSize', 12);
% Plot the layout as circles
t = linspace(0, 2 * pi);
plot(ro * cos(t) + center(1), ro * sin(t) + center(2));
grid on
hold(hax, 'on');
axis(hax, 'equal');
% Store Users assigned to each Base Station
UEs_per_BS = cell(NumBSs, 1);
for bs = 1:NumBSs
UEs_per_BS{bs} = PosUE(inrange & BSidx == bs, :);
end
% Display the Users assigned to each Base Station
for bs = 1:NumBSs
fprintf('Users assigned to Base Station %d:\n', bs);
disp(UEs_per_BS{bs});
end
Users assigned to Base Station 1:
781.1525 -105.7864 703.1062 162.8863 489.3488 191.6485 560.5379 173.2031
Users assigned to Base Station 2: Users assigned to Base Station 3:
132.9531 555.0479
Users assigned to Base Station 4:
248.9261 -917.8350 66.2251 -926.2438
Users assigned to Base Station 5:
114.5183 -477.6416 78.0318 -412.2890
Users assigned to Base Station 6:
-789.1126 -120.4379 -820.1432 50.5219 -781.0947 109.3466

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by