- Fixed Nodes Loop: The loop for fixed nodes remains unchanged except for the initialization of the arrays.
- 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.
- 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.
How compute connected node with each main node
3 ビュー (過去 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
0 件のコメント
回答 (1 件)
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:
Make sure to define "communication_range" appropriately based on your specific requirements.
Hope it helps.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!