for loop execution problem

for the code below pls ignore all except colonysize my problem is my colonysize is 6 but when i execute colonsize/2 i have to get 3 but for the sample of code below its getting as 1 when i write the code as
for i = 1:colonysize/2;
j = randperm(siz);
k = randperm(colonysize/2);
but for below case when j is initiaised first it works fine
for j = randperm(siz)
i = 1:colonysize/2;
k = randperm(colonysize/2);
pi = randi([-1 1])*rand;
emp_bee(i,j) = bee(i,j) + (pi * (bee(i,j)-bee(k,j)));
end
bee(i,j) is a 3x5 matrix

2 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 11 月 3 日
編集済み: Azzi Abdelmalek 2012 年 11 月 3 日
what is siz
there is an end missing
PK
PK 2012 年 11 月 3 日
siz=5

サインインしてコメントする。

回答 (1 件)

Wayne King
Wayne King 2012 年 11 月 3 日

0 投票

I do not have any difficulty with either of these for loops
colonysize = 6;
siz = 5;
bee = randn(3,5);
for i = 1:colonysize/2;
j = randperm(siz);
k = randperm(colonysize/2);
end
Or
for j = randperm(siz)
i = 1:colonysize/2;
k = randperm(colonysize/2);
pip = randi([-1 1])*rand;
emp_bee(i,j) = bee(i,j) + (pip * (bee(i,j)-bee(k,j)));
end
I changed pi to pip above. Are you sure you want to overwrite pi in MATLAB?

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

PK
2012 年 11 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by