user3075653
user3075653

Reputation: 149

Why is my script erroring with "Error:Index exceeds matrix dimensions"?

I made this script:

disp('Answer>>There are 25 prime numbers exist between 1-100')
x=isprime(1:100);
sum(x)

For some reason, it runs just once. For example, this script has the file name "question4", so when I type it in the main command, it prints: "There are.... ans=25" - which is perfect! but when I type again "question4", I get this error:

Index exceeds matrix dimensions.
Error in question4 (line 3)
sum(x)

Any reason why?

Upvotes: 1

Views: 426

Answers (1)

chappjc
chappjc

Reputation: 30589

Be careful not to use sum or any other built-in function name as a variable. To see if you are using it as a variable, check with the following:

>> which sum
sum is a variable.

If your output looks like the above, the variable sum is preventing the built-in sum function from being called as intended. Try clear sum and make sure you are not assigning sum anywhere in your script.

Upvotes: 2

Related Questions