Sum of a Vector using a for loop

1 Ansicht (letzte 30 Tage)
Abraham Robledo
Abraham Robledo am 7 Feb. 2019
How can I find the sum of vector x=[3 5 12 42 67], using a simple for loop?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Feb. 2019
Hint:
  • initialize a variable to the identity element for the operation you are using with respect to the field or group that you are calculating over
  • loop over all elements of the array
  • inside the loop, replace the variable with OPERATION applied between the variable and the loop value
  • at the end of the loop, the variable holds the answer.
  4 Kommentare
Adam Juckniewitz
Adam Juckniewitz am 6 Nov. 2019
I have similar homework. That advice is pretty helpful!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by