MATLAB Answers

How to obtain the original matrix of a cumsum()?

1 ビュー (過去 30 日間)
Philippe Corner
Philippe Corner 2018 年 3 月 21 日
コメント済み: Giacomo Tabarelli 2020 年 5 月 29 日
if we have a matrix B = cumsum(A);
B=[0.102493854430154 0.107645445153016 0.109982543018989 0.111250846129182 0.112023941002529
0.252983718693220 0.267537140617764 0.274291923474296 0.277936513591507 0.280166731981901
0.446994905411664 0.475357155580891 0.488884534539884 0.496152338879191 0.500676903345327
0.681174961701521 0.727902668424241 0.750721436460152 0.763062453240230 0.770697210445687
0.954490719298870 1.02481028893085 1.05975275214650 1.07879511204532 1.09055541700809];
how to obtain the "cumdiff" of the matrix B and obtain A.
Answer is:
A=[0.102493854430154,0.107645445153016,0.109982543018989,0.111250846129182,0.112023941002529;0.150489864263066,0.159891695464748,0.164309380455307,0.166685667462325,0.168142790979372;0.194011186718444,0.207820014963127,0.214592611065588,0.218215825287684,0.220510171363426;0.234180056289857,0.252545512843350,0.261836901920268,0.266910114361039,0.270020307100360;0.273315757597349,0.296907620506609,0.309031315686344,0.315732658805087,0.319858206562400];

  0 件のコメント

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

採用された回答

John D'Errico
John D'Errico 2018 年 3 月 21 日
Just use diff! Then append the first element.
A = rand(1,10);
B = cumsum(A);
C = [B(1), diff(B)];

  3 件のコメント

Giacomo Tabarelli
Giacomo Tabarelli 2020 年 5 月 28 日
Could you explain me the strange behaviour of test.m? Why the first compare is true for all entries instead the second do not?
In the second case using diff even selecting one row at once or all the matrices together leads to the same result.
The diff command appears to lose some precision during operations.
John D'Errico
John D'Errico 2020 年 5 月 28 日
Then it is time for you to learn about floating point numbers, about how computations are done using computers in floating point arithmetic. And... why you should NEVER trust the least significant bits of a number done in such a computation, UNLESS you know enough about what you are doing that you fully understand why not to trust those least significant bits. And even then, don't trust those least significant bits.
dW(1,:)
ans =
-0.11486 0.0044322 0.027686 -0.069706 -0.17065 -0.23663 0.21574 -0.041219 0.093753
>> diff(W(1,:))
ans =
-0.11486 0.0044322 0.027686 -0.069706 -0.17065 -0.23663 0.21574 -0.041219 0.093753
Are they identically the same? No.
abs(dW(1, :) - diff(W(1, :), 1, 2))
ans =
0 4.3368e-18 6.9389e-18 1.3878e-17 0 0 0 0 0
Are they significantly different? No. NEVER test for exact equality in something like that.
Giacomo Tabarelli
Giacomo Tabarelli 2020 年 5 月 29 日
Ok I know about floating point numbers. My question is why there is such a difference? Is there a way to avoid this errors? I need this to compute SDE convergence errors. I found problems in the order of convergence and investigating I found this. Any suggestion?

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

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by