How to use the SUM() function..?

Hello,
I must be doing something very stupid here, as I can't get the sum() function to work at all;
X = [0 0.5 1 1.5 2 2.5]
sum(X(1,:))
From what I've read, this should sum all the element of row 1 of the matrix X and return a numerical result, but when I try it, I get the error:
Array indices must be positive integers or logical values.
Can anybody tell me what I'm doing wrong, please?
Thanks,
Paul

 Respuesta aceptada

Bruno Luong
Bruno Luong el 5 de Oct. de 2022
You overshadow sum function
make sure to run
clear sum
before runing your code

2 comentarios

paul
paul el 5 de Oct. de 2022
Movida: Steven Lord el 5 de Oct. de 2022
...As is so often the way, I almost instantly found the problem after bashing my head against it and then posting my question...
I cleared all my workspace variables and suddenly sum(0 started working, so there must have been something in there which was stopping sum() from working...
Thanks to anybody who was thinking about this for me,
Paul
paul
paul el 5 de Oct. de 2022
Movida: Steven Lord el 5 de Oct. de 2022
Thanks very much :)

Iniciar sesión para comentar.

Más respuestas (1)

Torsten
Torsten el 5 de Oct. de 2022

0 votos

Did you create a MATLAB function with name sum.m ? If yes, rename it.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Productos

Versión

R2022a

Etiquetas

Preguntada:

el 5 de Oct. de 2022

Movida:

el 5 de Oct. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by