How to use a for loop to calculate the sum of a row vector?

261 vues (au cours des 30 derniers jours)
Michelle S
Michelle S le 25 Nov 2015
Commenté : Yadu Bhusal le 14 Août 2021
I have the values x-[1, 23, 43, 72, 87, 56, 98, 33] How do you calculate the sum (which should equal 413) using a for loop?

Réponse acceptée

Stalin Samuel
Stalin Samuel le 25 Nov 2015
Modifié(e) : Stephen23 le 21 Déc 2018
x = [1, 23, 43, 72, 87, 56, 98, 33] ;
y = 0;
for n = 1:length(x)
y = y + x(n);
end
  8 commentaires
Christopher Vargas
Christopher Vargas le 6 Avr 2020
disp(y) %this will display the result
Lexi Finke
Lexi Finke le 5 Oct 2020
how do you make this loop into a function??

Connectez-vous pour commenter.

Plus de réponses (1)

Aza Xongo
Aza Xongo le 16 Avr 2019
Modifié(e) : Aza Xongo le 16 Avr 2019
Write a program (using loops) that calculates and displays the sum of all the elements of x=[2 -5 6 7; -4 8 -5 6];(its a question)
  1 commentaire
Yadu Bhusal
Yadu Bhusal le 14 Août 2021
X =['enter an array>']; m = length(x); Sum = 0; For i = 1:m Sum = sum + X(i); end Disp(sum)
IF you want single sum. It you wish iterations remove semicolon from end of X(i).

Connectez-vous pour commenter.

Catégories

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by