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 次查看(过去 30 天)
显示 更早的评论
%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]);
0 个评论
回答(1 个)
Vaibhav
2024-4-18
编辑:Vaibhav
2024-4-18
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!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!