简体   繁体   中英

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)

And from 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?? Thanks in advanced!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM