How compute connected node with each main node

2 次查看(过去 30 天)
I need to compute the number of green nodes and number of red nodes connected with each fixed nodes as shown in figure below (ie. for fixed node 1 in top left there are 14 green node and 0 red node while in node 4 in middle there are 9 green nodes and 5 red nodes connected with it
the code is in below
%Composing fixed nodes t
for h=1:1:num_fixed_nodes
S1(h).xd = position_region(i,1) + fixed_nodes{i, 1}(h,1);
S1(h).yd = position_region(i,2) + fixed_nodes{i, 1}(h,2);
S1(h).distance = sqrt((S1(h).xd-bs_x)^2 + (S1(h).yd-bs_y)^2 );
S1(h).G=0;
S1(h).id=h;
S1(h).type='C';
S1(h).temp = interp2(aa1,bb1,temp_values_1,S1(h).xd,S1(h).yd);
S1(h).E = Eo*rand(1,1);
Et=Et+S1(h).E;
S1(h).node_status = S1(h).temp<thresh_temp;
if(S1(h).node_status==1)
scatter(S1(h).xd,S1(h).yd, 'filled','MarkerFaceColor','m');
else
scatter(S1(h).xd,S1(h).yd, 'filled','MarkerFaceColor','r');
end
end
%Composing random nodes that will then communicate with fixed
%nodes
for h=num_fixed_nodes+1:1:num_nodes
%Generate random nodes within the region
S1(h).xd = position_region(i,1) + rand(1,1)*region_width;
S1(h).yd = position_region(i,2) + rand(1,1)*region_height;
S1(h).G=0;
S1(h).id=h;
S1(h).type='N';
S1(h).temp = interp2(aa1,bb1,temp_values_1,S1(h).xd,S1(h).yd);
S1(h).E=Eo*rand(1,1);
Et=Et+S1(h).E;
S1(h).node_status = S1(h).temp<thresh_temp;
if(S1(h).node_status==1)
scatter(S1(h).xd,S1(h).yd, 'filled','MarkerFaceColor','g');
else
scatter(S1(h).xd,S1(h).yd, 'filled','MarkerFaceColor','r');
end

回答(1 个)

surya venu
surya venu 2024-7-10
编辑:surya venu 2024-7-10
Hi,
To compute the number of green nodes and red nodes connected with each fixed node, you can add additional code to count the nodes based on their status and their proximity to each fixed node. Below is a modified version of your code that includes this functionality:
% Initialize arrays to store the counts of green and red nodes for each fixed node
green_count = zeros(num_fixed_nodes, 1);
red_count = zeros(num_fixed_nodes, 1);
% Composing fixed nodes
for h = 1:num_fixed_nodes
S1(h).xd = position_region(i, 1) + fixed_nodes{i, 1}(h, 1);
S1(h).yd = position_region(i, 2) + fixed_nodes{i, 1}(h, 2);
S1(h).distance = sqrt((S1(h).xd - bs_x)^2 + (S1(h).yd - bs_y)^2);
S1(h).G = 0;
S1(h).id = h;
S1(h).type = 'C';
S1(h).temp = interp2(aa1, bb1, temp_values_1, S1(h).xd, S1(h).yd);
S1(h).E = Eo * rand(1, 1);
Et = Et + S1(h).E;
S1(h).node_status = S1(h).temp < thresh_temp;
if S1(h).node_status == 1
scatter(S1(h).xd, S1(h).yd, 'filled', 'MarkerFaceColor', 'm');
else
scatter(S1(h).xd, S1(h).yd, 'filled', 'MarkerFaceColor', 'r');
end
end
% Composing random nodes that will then communicate with fixed nodes
for h = num_fixed_nodes + 1:num_nodes
% Generate random nodes within the region
S1(h).xd = position_region(i, 1) + rand(1, 1) * region_width;
S1(h).yd = position_region(i, 2) + rand(1, 1) * region_height;
S1(h).G = 0;
S1(h).id = h;
S1(h).type = 'N';
S1(h).temp = interp2(aa1, bb1, temp_values_1, S1(h).xd, S1(h).yd);
S1(h).E = Eo * rand(1, 1);
Et = Et + S1(h).E;
S1(h).node_status = S1(h).temp < thresh_temp;
if S1(h).node_status == 1
scatter(S1(h).xd, S1(h).yd, 'filled', 'MarkerFaceColor', 'g');
else
scatter(S1(h).xd, S1(h).yd, 'filled', 'MarkerFaceColor', 'r');
end
% Check proximity to each fixed node and update counts
for fixed_h = 1:num_fixed_nodes
distance_to_fixed = sqrt((S1(h).xd - S1(fixed_h).xd)^2 + (S1(h).yd - S1(fixed_h).yd)^2);
if distance_to_fixed <= communication_range % Assuming a predefined communication range
if S1(h).node_status == 1
green_count(fixed_h) = green_count(fixed_h) + 1;
else
red_count(fixed_h) = red_count(fixed_h) + 1;
end
end
end
end
% Display the counts
for h = 1:num_fixed_nodes
fprintf('Fixed node %d: %d green nodes, %d red nodes\n', h, green_count(h), red_count(h));
end
Explanation:
  1. Fixed Nodes Loop: The loop for fixed nodes remains unchanged except for the initialization of the arrays.
  2. Random Nodes Loop: In addition to the existing operations, this loop now includes a nested loop to check the proximity of each random node to each fixed node.
  3. Distance Check: For each random node, the distance to each fixed node is calculated. If the distance is within a predefined "communication_range", the node is counted as either green or red based on its status.
Make sure to define "communication_range" appropriately based on your specific requirements.
Hope it helps.
  1 个评论
muhammad farttoos
muhammad farttoos 2024-7-18
Hello,
Thanks for the reply and sorry for the delay, I just need to ask what "communication_range" means. I distributed nodes randomly within area (50x50) and nodes is depends where it located within area so how defined "communication_range"??

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by