- rand: https://www.mathworks.com/help/matlab/ref/rand.html
- randperm: https://www.mathworks.com/help/matlab/ref/randperm.html
- zeros: https://www.mathworks.com/help/matlab/ref/zeros.html
Ensuring each node becoming as CH exactly one time in LEACH
1 view (last 30 days)
Show older comments
I am implementing my LEACH protocol. I want each of 100 nodes become as a CH exactly one time or in one round among 100 rounds. Below is the code snippet for CH making. In this code nodes become multiple time as CH, but i want exactly once. How to make it. Selecting one node uniquely based on probability in each round seems tricky.
close all;
clear all;
clc;
num_nodes = 100;
rounds = 100;
probability_threshold = 0.1;
% Initialize cluster heads
cluster_heads = zeros(rounds, num_nodes);
for r = 1:rounds
% Select cluster heads probabilistically
for i = 1:num_nodes
if rand <= probability_threshold %need to make exactly one node satisfy the condition
cluster_heads(r, i) = 1; % Node becomes a cluster head
end
end
end
Pls clarify!
0 Comments
Answers (1)
Paras Gupta
on 22 Jun 2024
Hey,
I understand that you want to ensure each node is selected exactly once as a Cluster Head (CH) while using a probability threshold. We can achieve this by maintaining a list of nodes that have already been selected and ensuring they are not selected again in subsequent rounds.
Here's an updated version of your code to accomplish this:
close all;
clear all;
clc;
num_nodes = 100;
rounds = 100;
probability_threshold = 0.1;
% Initialize cluster heads
cluster_heads = zeros(rounds, num_nodes);
selected_nodes = false(1, num_nodes); % Track selected nodes
for r = 1:rounds
while true
% Select a node probabilistically
for i = 1:num_nodes
if ~selected_nodes(i) && rand <= probability_threshold
cluster_heads(r, i) = 1; % Node becomes a cluster head
selected_nodes(i) = true; % Mark node as selected
break;
end
end
% Check if a node was selected in this round
if any(cluster_heads(r, :))
break;
end
end
end
This approach ensures that each node is selected exactly once as a CH while still using a probability threshold.
Alternatively, we can use a deterministic approach by leveraging the 'randperm' function to generate a random permutation of nodes, ensuring each node is selected exactly once in 100 rounds. Here’s how you can do it:
close all;
clear all;
clc;
num_nodes = 100;
rounds = 100;
% Initialize cluster heads
cluster_heads = zeros(rounds, num_nodes);
% Create a permutation of nodes to ensure each node becomes CH exactly once
nodes_permutation = randperm(num_nodes);
for r = 1:rounds
% Select the node that becomes a cluster head for this round
selected_node = nodes_permutation(r);
cluster_heads(r, selected_node) = 1; % Node becomes a cluster head
end
You can refer to the following documentation for more information on the functions used in the code above:
Hope this resolves your query.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!