Bizarre result of the sum() function in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter mikeph
  • Start date Start date
  • Tags Tags
    Function Matlab Sum
Click For Summary
SUMMARY

The forum discussion centers on an issue with the MATLAB sum() function returning unexpected results due to variable shadowing. The user, Michael, encountered erroneous outputs when summing a vector of ones, which were resolved by using the clear all command. It was determined that a previously defined variable or function named sum was likely overriding the built-in function, leading to incorrect calculations. The discussion emphasizes the importance of managing variable names to avoid conflicts in MATLAB.

PREREQUISITES
  • Understanding of MATLAB syntax and commands
  • Familiarity with variable scope and shadowing in programming
  • Knowledge of MATLAB data types, specifically vectors
  • Experience with MATLAB's command window and script editor
NEXT STEPS
  • Learn about variable shadowing in MATLAB and how to avoid it
  • Explore the use of the clear command and its variations in MATLAB
  • Investigate MATLAB's built-in functions and how to check for name conflicts
  • Practice writing and debugging MATLAB scripts in the editor for better code management
USEFUL FOR

MATLAB users, data analysts, and programmers who are troubleshooting function conflicts and optimizing their coding practices in MATLAB.

mikeph
Messages
1,229
Reaction score
18
Hello,

Am was trying to figure out why some code was not working and narrowed it down to a little loop that was not being executed.

x is a 4x1 vector with 0 or 1 as each element, eg. x = [0; 0; 1; 1]. The code was required to sum the columns and if this sum was positive (ie. if there were any nonzero elements) it would execute another loop.

The vector was [1; 1; 1; 1] and it was not executing, so I tested the code. Here is copy pasted results of me experimenting and eventually fixing the problem using "clear all" command:


Code:
x =
     1
     1
     1
     1
EDU>> sum(x)
ans =
    1.4142    1.4142         0         0
EDU>> sum(x')
ans =
    1.4142    1.4142         0         0
EDU>> x=[1;1;1;1]
x =
     1
     1
     1
     1
EDU>> sum(x)
ans =
    1.4142    1.4142    1.4142    1.4142
EDU>> r = [1,2,3]
r =
     1     2     3
EDU>> sum(r)
ans =
    1.4142    1.4142         0
EDU>> clear all
EDU>> r = [1,2,3]
r =
     1     2     3
EDU>> sum(r)
ans =
     6

The "clear all" seems to fix everything. But I can't figure out what was wrong. I wasn't doing anything out of the ordinary. Anyone have any ideas what was going wrong?

Thanks
Michael
 
Physics news on Phys.org
You probably accidentally defined sum to mean something else some place earlier. This would shadow the built in function until you cleared it.
 
why are you trying to do this in the command window its easier to do this in a editor. just click the new script icon on the top left next to open project. this error might be because you had another variable name x that's messing with your results you can usually see in the workspace box. when you use clear all you release the data on all the variables you've been working on so far so that's why your new variable r is working
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K