while executing teachers phase of TLBO algorithm i am getting the following error, please rectify my mistake (Index in position 1 exceeds array bounds (must not exceed 1)).

2 vues (au cours des 30 derniers jours)
%learner phase
%to select a student randomly
sno=1+randi(ns,1,ns);
%to chech the same student not selected
for i=1:ns
while (i==sno(i,1))
sno(i,1)=1+randi(ns,1,ns);
end
end
disp('selected student number:');disp('------');
disp([(1:ns)' sno]);

Réponses (1)

Vaibhav
Vaibhav le 18 Avr 2024
Modifié(e) : Vaibhav le 18 Avr 2024
Hi Venkatesh
The issue lies in the loop where you are updating the sno array. Specifically, the condition (i == sno(i, 1)) is causing the problem. Since sno(i, 1) is always equal to i (due to the assignment inside the loop), the loop becomes infinite.
To rectify this, you can modify the loop as follows:
% Learner phase: To select a student randomly
sno = randi(ns, 1, ns); % Initialize sno without adding 1
% Check that the same student is not selected
for i = 1:ns
while (i == sno(i))
sno(i) = randi(ns); % Update sno directly
end
end
We initialize sno without adding 1 initially, and then directly update its elements within the loop.
Hope this helps!

Catégories

En savoir plus sur Language Fundamentals dans Help Center et File Exchange

Tags

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by