WHILE LOOP program does not work

3 vues (au cours des 30 derniers jours)
sss dzu
sss dzu le 24 Oct 2012
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 commentaire
Walter Roberson
Walter Roberson le 24 Oct 2012
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.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 25 Oct 2012
Try it without loops, in a more MATLAB-ish vectorized way:
indexesToSum = a>=4 & a<=15
Re = sum(a(indexesToSum))
  1 commentaire
sss dzu
sss dzu le 25 Oct 2012
thank you

Connectez-vous pour commenter.

Plus de réponses (2)

Matt Fig
Matt Fig le 24 Oct 2012
Modifié(e) : Matt Fig le 24 Oct 2012
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 commentaire
sss dzu
sss dzu le 25 Oct 2012
I tried before, but it did not work either

Connectez-vous pour commenter.


sss dzu
sss dzu le 25 Oct 2012
but i have to use while loop

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by