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)).

1 回表示 (過去 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]);

回答 (1 件)

Vaibhav
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!

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by