| New Reply |
Bizarre result of the sum() function in MATLAB |
Share Thread | Thread Tools |
| Mar16-10, 11:55 AM | #1 |
|
|
Bizarre result of the sum() function in MATLAB
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
Thanks Michael |
| Mar17-10, 02:03 AM | #2 |
|
|
You probably accidentally defined sum to mean something else some place earlier. This would shadow the built in function until you cleared it.
|
| Jun6-12, 10:44 AM | #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 thats 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 thats why your new variable r is working
|
| New Reply |
| Thread Tools | |
Similar Threads for: Bizarre result of the sum() function in MATLAB
|
||||
| Thread | Forum | Replies | ||
| DFT Matlab function | Math & Science Software | 0 | ||
| MATLAB Write result to txt file | Math & Science Software | 5 | ||
| Does a function exist (prefferably in matlab) that finds a function in noise | Math & Science Software | 4 | ||
| Matlab function | Math & Science Software | 2 | ||
| What if complex biological systems emerged as a result of a wave function? | General Physics | 4 | ||