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

조회 수: 2 (최근 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];

채택된 답변

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
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개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by