Hello,(adsbygoogle = window.adsbygoogle || []).push({});

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:

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?Code (Text):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

**Physics Forums - The Fusion of Science and Community**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Bizarre result of the sum() function in MATLAB

Loading...

Similar Threads - Bizarre result function | Date |
---|---|

Mathematica Cannot do the integral of the Hyper-geometric function? | Mar 11, 2018 |

Mathematica Another bug? Weird integration results. | Apr 16, 2016 |

Online function grapher with downloadable result values? | Oct 8, 2015 |

MATLAB program results discrepancy... | May 12, 2015 |

Unexpected result of the "find" command in Matlab | Feb 19, 2015 |

**Physics Forums - The Fusion of Science and Community**