フィルターのクリア

WHILE LOOP program does not work

1 回表示 (過去 30 日間)
sss dzu
sss dzu 2012 年 10 月 24 日
Please, any one tell me why my program does not work, thank you.
This is my program
a=[7 18 11 5;
2 8 10 13;
3 24 20 12;
21 1 20 17];
[m,n]= size(a);
Re=0;
i=1;
while i<=m;
j=1
while j<=n;
if ((a(i,j)<=15) & (a(i,j)>=4))
Re= Re+ a(i,j);
end
end
end
disp(Re)
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 10 月 24 日
In order for people to tell you why your program does not work, you have to tell them what the intended working of the program is.

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

採用された回答

Image Analyst
Image Analyst 2012 年 10 月 25 日
Try it without loops, in a more MATLAB-ish vectorized way:
indexesToSum = a>=4 & a<=15
Re = sum(a(indexesToSum))
  1 件のコメント
sss dzu
sss dzu 2012 年 10 月 25 日
thank you

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

その他の回答 (2 件)

Matt Fig
Matt Fig 2012 年 10 月 24 日
編集済み: Matt Fig 2012 年 10 月 24 日
You do not increment i or j inside the loops.
i =i+1;
etc. Thus i will always be less than m, and j less than n.
  1 件のコメント
sss dzu
sss dzu 2012 年 10 月 25 日
I tried before, but it did not work either

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


sss dzu
sss dzu 2012 年 10 月 25 日
but i have to use while loop

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by