フィルターのクリア

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

2 ビュー (過去 30 日間)
Abalfazl Zareei
Abalfazl Zareei 2013 年 4 月 10 日
hi, I have a vector w and it is a matrix with one row and 10 columns.
>> sum(w)
ans =
-0.1563
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 =
1
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

回答 (3 件)

James Tursa
James Tursa 2013 年 4 月 11 日
編集済み: James Tursa 2013 年 4 月 11 日
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:
eps(max(abs(w)))
And then compare that result with your answer and you will see that they are about the same size.
  1 件のコメント
Jan
Jan 2013 年 4 月 11 日
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
sum(w)
sum(sort(w))
[dummy, index] = sort(abs(w));
sum(w(index))
Do you see a difference already? For a sum with error correction see FEX: XSum

サインインしてコメントする。


bym
bym 2013 年 4 月 10 日
try
ones(size(w))*w.'
  2 件のコメント
Cedric
Cedric 2013 年 4 月 10 日
編集済み: Cedric 2013 年 4 月 10 日
It's not that; it is related to the way MATLAB computes the product between a vector whose elements are ~1e15 and an other whose elements are 1's.
Abalfazl Zareei
Abalfazl Zareei 2013 年 4 月 10 日
The same answer:
>> ones(size(w))*w.'
ans =
1

サインインしてコメントする。


Ahmed A. Selman
Ahmed A. Selman 2013 年 4 月 10 日
The line
ones(1,size(w,2))*w'
means creating a ones matrix with dimensions (1,size(w,2)) multiplied with w', and
sum(w)
means summation of the vector w.
I really don't know if it is right to compare these two, entirely different things.
So use
sum(ones(1,size(w,2))*w')
and compare :)
  2 件のコメント
Jan
Jan 2013 年 4 月 11 日
編集済み: Jan 2013 年 4 月 11 日
Multiplying an [1xN] with an [Nx1] vector means the dot-product. This is mathematically the same as summing the multiplied elements. Using a vector of 1's as one of the vectors results in a sum. Therefore the shown procedures are not different in theory.
Ahmed A. Selman
Ahmed A. Selman 2013 年 4 月 11 日
Indeed it is the same. I thought w was N-by-N, my bad. Thank you for the correction.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by