Bizarre result of the sum() function in MATLAB

In summary, Michael was trying to fix an issue with a code that sums up columns in a vector. He found that the code was not executing properly and tested it with different vectors. Eventually, he discovered that the problem was caused by accidentally defining the sum function as something else earlier in the command window. Clearing all variables fixed the issue. Someone suggested using an editor instead of the command window for easier debugging.
  • #1
mikeph
1,235
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
  • #2
You probably accidentally defined sum to mean something else some place earlier. This would shadow the built in function until you cleared it.
 
  • #3
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
 

1. Why is the sum() function in MATLAB giving me a bizarre result?

There could be several reasons for this. It could be due to incorrect data input, improper use of the function, or a bug in the program. It is important to check your code and input data carefully to pinpoint the issue.

2. How can I fix the bizarre result I am getting from the sum() function in MATLAB?

First, try to identify the source of the issue by checking your code and input data. If the problem persists, it could be a bug in the program. In that case, you can try updating your MATLAB version or reaching out to the support team for assistance.

3. Is there a specific data type that the sum() function in MATLAB works best with?

The sum() function in MATLAB is designed to work with numerical data types such as integers, floating-point numbers, and complex numbers. It may not work as expected with other data types such as strings or characters.

4. Can I use the sum() function in MATLAB to add multiple arrays together?

Yes, the sum() function in MATLAB can be used to add multiple arrays together. However, the arrays must have the same dimensions for the function to work properly. If the arrays have different dimensions, you may need to use the cat() function to concatenate them before using the sum() function.

5. How can I make the sum() function in MATLAB ignore NaN (Not a Number) values?

You can use the 'omitnan' option in the sum() function to ignore NaN values. This option is available in newer versions of MATLAB. If you have an older version, you can use the 'includenan' option to include NaN values in the calculation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
572
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
939
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
29
Views
4K
Back
Top