1

I have 2 vectors of size 1x90.

I have to do the operator

diff=sum((V_new-V).^2);

But every time i do it i get the error:

Subscript indices must either be real positive integers or logical.

How can i fix this problem and prevent it from occurring again?

ECE
  • 404
  • 5
  • 13
  • possible duplicate of [Subscript indices must either be real positive integers or logicals](http://stackoverflow.com/questions/15339173/subscript-indices-must-either-be-real-positive-integers-or-logicals) – Shai Mar 14 '13 at 17:09
  • I think the reason is that you use `sum` as a variable before `diff=sum((V_new-V).^2);`. – tqjustc Mar 14 '13 at 17:33
  • @tqjustc - have you bothered to look at other answers here before commenting? – Shai Mar 14 '13 at 17:41
  • Also see [this question](http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol) for [the generic solution to this problem](http://stackoverflow.com/a/20054048/983722). – Dennis Jaheruddin Nov 27 '13 at 16:03

1 Answers1

3

make sure you did not step over the sum function:

  1. type

    >> dbstop if error
    
  2. run the code, it should stop in debugger when error occurred.

  3. check what sum is:

    >> which sum
    

    should return that sum is a build-in function, but if you accidentally created a variable with that name, it will tell you that sum is a variable.

DO NOT USE BUILT-IN FUNCTIONS' NAMES AS VARIABLES

Shai
  • 111,146
  • 38
  • 238
  • 371
  • Can you please clarify what you mean by "step over" the sum function please? – ECE Mar 14 '13 at 17:04
  • A more apt title should be: Take the difference of every element of the 2 vectors square them and then sum all those values together. – ECE Mar 14 '13 at 17:05
  • @ECE - is it possible you created a variable with the name `sum`? – Shai Mar 14 '13 at 17:06
  • 3
    "DO NOT USE BUILT-IN FUNCTIONS' NAMES AS VARIABLES": same goes for **diff**, which is a built-in function. – Milo Mar 14 '13 at 17:07
  • In fact, you could have used the built-in function **diff** to calculate the difference between the vectors: diff(V_new, V) – Milo Mar 14 '13 at 17:09
  • I did have a variable by the name sum, and diff and i changed both. Here is the new command: difference_value=sum((V_new-V).^2); – ECE Mar 14 '13 at 17:16
  • 1
    run `clear sum` before you run your code. Make sure (in debug) that `sum` points to the built-in function – Shai Mar 14 '13 at 17:26
  • thank you! i have to do clear sum and clear diff. I appreciate it Shai! – ECE Mar 14 '13 at 17:33