implement a script that calculates 1^3+2^3+...+10^3+11^3. a) use a for or a while loop. b)use vectors
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Chloe St John
el 24 de En. de 2019
Comentada: Rena Berman
el 5 de Mzo. de 2019
thank you !
1 comentario
Respuesta aceptada
Kevin Phung
el 24 de En. de 2019
Editada: Kevin Phung
el 24 de En. de 2019
x = 1:11;
%for loop
sum=0;
for i = 1:numel(x)
sum = sum + x(i)^3;
end
%vector method
vec = sum(x.^3);
3 comentarios
Jan
el 28 de En. de 2019
Editada: Jan
el 28 de En. de 2019
@Kevin: Your code will fail, because you have defined "sum" as a variable at first and try to call the function sum() afterwards. Do not use the names of important built-in functions as names of variables.
This was a homework question obviously. Now the OP has removed the question, such that this threads becomes meaningless - the 3rd time. This is a counter-productive and impolite behaviour.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!