Effacer les filtres
Effacer les filtres

sum inside a for loob

1 vue (au cours des 30 derniers jours)
tevzia
tevzia le 23 Juil 2013
hi everyone, I was wondering how to sum my calculation inside loop. here is a simple code:
%
for i=1:5
a = price1 * i;
b =sum(a)
%
so how to sum a, when i type like this it gives result (b=a) for every times. It does not add(sum) all of the results
  1 commentaire
tevzia
tevzia le 23 Juil 2013
I figured out
% %
a=0;
for i =1:5
a = a + (price1 * i)
% code
end

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 23 Juil 2013
b = 0;
for k = 1:5
a = price1 * k;
b = b + sum(a); % Or b = b + a?
end

Plus de réponses (0)

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by