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

222 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Stalin Samuel
Stalin Samuel el 25 de Nov. de 2015
Editada: Stephen23 el 21 de Dic. de 2018
x = [1, 23, 43, 72, 87, 56, 98, 33] ;
y = 0;
for n = 1:length(x)
y = y + x(n);
end
  8 comentarios
Christopher Vargas
Christopher Vargas el 6 de Abr. de 2020
disp(y) %this will display the result
Lexi Finke
Lexi Finke el 5 de Oct. de 2020
how do you make this loop into a function??

Iniciar sesión para comentar.

Más respuestas (1)

Aza Xongo
Aza Xongo el 16 de Abr. de 2019
Editada: Aza Xongo el 16 de Abr. de 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 comentario
Yadu Bhusal
Yadu Bhusal el 14 de Ag. de 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).

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by