sum(w) and ones(1,size(w,2))*w' results totally different numbers

3 views (last 30 days)
Abalfazl Zareei
Abalfazl Zareei on 10 Apr 2013
hi, I have a vector w and it is a matrix with one row and 10 columns.
>> sum(w)
ans =
Then, I calculate the following, which is the product of a Ones vector and w, in which in matrix algebra, this is equivalent to summation of w's elements. >> ones(1,size(w,2))*w'
ans =
Both results are different. Would you please let me know why?
w =
1.0e+15 *
Columns 1 through 6
-0.0497 2.4484 -3.2273 0.1944 0.1592 0.4407
Columns 7 through 10
-0.4389 0.1548 0.1592 0.1592

Answers (3)

James Tursa
James Tursa on 11 Apr 2013
Edited: James Tursa on 11 Apr 2013
The answer is on the order of eps of the numbers you are dealing with. You have massive cancellation going on in the operation, and the order of the operation will make a difference (apparently sum and mtimes are doing the calculation in a slightly different order). The answer will have a lot of garbage bits. E.g., what do you get when you do this:
And then compare that result with your answer and you will see that they are about the same size.
  1 Comment
Jan on 11 Apr 2013
Exactly. Try this (insert all digits, if you have them):
w = [-0.0497e15, 2.4484e15, -3.2273e15, 0.1944e15, 0.1592e15, ...
0.4407e15, -0.4389e15, 0.1548e15, 0.1592e15, 0.1592e15];
format long g
[dummy, index] = sort(abs(w));
Do you see a difference already? For a sum with error correction see FEX: XSum

Sign in to comment.

bym on 10 Apr 2013

Sign in to comment.

Ahmed A. Selman
Ahmed A. Selman on 10 Apr 2013
The line
means creating a ones matrix with dimensions (1,size(w,2)) multiplied with w', and
means summation of the vector w.
I really don't know if it is right to compare these two, entirely different things.
So use
and compare :)
Ahmed A. Selman
Ahmed A. Selman on 11 Apr 2013
Indeed it is the same. I thought w was N-by-N, my bad. Thank you for the correction.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!