Clear Filters
Clear Filters

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 views (last 30 days)
%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]);

Answers (1)

Vaibhav
Vaibhav on 18 Apr 2024
Edited: Vaibhav on 18 Apr 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!

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!