i need to calculate fitness value and save that in separate .mat file

4 visualizaciones (últimos 30 días)
SARAH LONER
SARAH LONER el 22 de En. de 2020
Editada: Samayochita el 11 de Feb. de 2025 a las 11:16
n=number of node (i have 100 nodes)
mch = cluster head per each round of node (here i gave number of round =30 nd i get 14 cluster head nodes) % didnt take repeted cluster heads
residual energy = sum of energy for 30 rounds
p1 and p2= randam numbers between 0 abd 1
i store mch in mat nd residualenergy into another mat file
i need to calculate each time fitness value for 100 nodes based on my cluster heads and residual energy
for i=1:n
Fit = p1 .*(n.* (mch(i,:))) + p2 .*sum (resdialenergy(i,:))
end
error at
Index in position 1 exceeds array bounds.
Error in ch_rnd (line 305)
Fit = p1 .*(n.* (mch(i,:))) + p2 .*sum (resdialenergy(i,:))
i need to sort this kindly help me its urgent

Respuestas (1)

Samayochita
Samayochita el 11 de Feb. de 2025 a las 11:16
Editada: Samayochita el 11 de Feb. de 2025 a las 11:16
Hi SARAH,
There are a couple of issues with your code:
  • I explicitly checked the structure of “mch” and “residualenergy” by using “fieldnames(data_mch)” and “disp(data_residual)” to understand how to access the data. This helped in correctly extracting “mch” and “residualenergy”.
  • The “mch” data is stored as a structure array (data_mch.CH), the correct approach would be to loop through it to extract the IDs.
  • Similarly, “residualenergy” should be accessed using its correct field name (SumEnergyAllSensor), ensuring the data is numeric.
  • Moreover, the “mch” data is being treated as a matrix and is being indexed inappropriately leading to indexing errors. It should be correctly extracted as a 1D numeric array of IDs.
  • You should directly use “sum(residualenergy)” to get the total energy across all rounds, as “residualenergy” is a 1D array with 31 elements.
  • The fitness calculation requires the sum of “mch” (cluster head IDs) and “residualenergy”. Since “mch” is a 1D array and “residualenergy” is also 1D, we sum over their entire values for each node's fitness calculation.
Here’s how you could make the changes:
  1. First load the data from “residualenergy.mat” and “mch.mat”:
data_residual = load('residualenergy.mat');
residualenergy = data_residual.SumEnergyAllSensor; % Use the correct field name
data_mch = load('mch.mat');
mch = zeros(1, length(data_mch.CH));
2. Here’s how to properly extract 'id' values and store them in the “mch” array:
for i = 1:length(data_mch.CH)
mch(i) = data_mch.CH(i).id; % Extract 'id' and store it in the array
end
3. The below code is used to check if “mch” and “residualenergy” are numeric:
if ~isnumeric(mch)
error('mch is not numeric.');
end
if ~isnumeric(residualenergy)
error('residualenergy is not numeric.');
end
4. You can calculate fitness for each node as follows:
for i = 1:n
% Modify the calculation to match the array dimensions
% Assuming mch and residualenergy are 1D arrays
Fit(i) = p1(i) * (n * sum(mch)) + p2(i) * sum(residualenergy);
end
5. Finally, you could display the fitness values to see the result or optionally save them in a .mat file as shown below:
disp('Calculated Fitness Values:');
disp(Fit);
save('fitness.mat', 'Fit');
Hope this helps!

Categorías

Más información sobre Least Squares en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by