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

2 views (last 30 days)
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];

Accepted Answer

John D'Errico
John D'Errico on 21 Mar 2018
Just use diff! Then append the first element.
A = rand(1,10);
B = cumsum(A);
C = [B(1), diff(B)];
  3 Comments
John D'Errico
John D'Errico on 28 May 2020
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 on 29 May 2020
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?

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!