Borrar filtros
Borrar filtros

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 visualizaciones (últimos 30 días)
%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]);

Respuestas (1)

Vaibhav
Vaibhav el 18 de Abr. de 2024
Editada: Vaibhav el 18 de Abr. de 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!

Categorías

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

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by