How can I solve the following error?

3 vues (au cours des 30 derniers jours)
Zeinab Hassanzadeh
Zeinab Hassanzadeh le 6 Juil 2022
Modifié(e) : Fangjun Jiang le 6 Juil 2022
For an arbitrary matrix, I want to sum the entries of each row of it.
I use the command sum(A,2) for the square matrix A. But I encounter the following error
"Index in position 1 is invalid. Array indices must be positive inteders or logical values"

Réponses (2)

Star Strider
Star Strider le 6 Juil 2022
Somewhere you have a variable named ‘sum’ and this is throwing the error.
The solution is to re-name the variable to something else that makes sense in the context of the code and does not overshadow any MATLAB function names.

Fangjun Jiang
Fangjun Jiang le 6 Juil 2022
Most likely, you have a variable called "sum". Clear it first and then run your code
clear sum;
A=magic(5);
sum(A,2)
ans = 5×1
65 65 65 65 65

Catégories

En savoir plus sur Creating and Concatenating Matrices 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