Making Cumulative Difference Calculation

30 ビュー (過去 30 日間)
Aadil
Aadil 2012 年 8 月 16 日
Hi,
I have a parameter with a column of data like this:
RunTime:
3779024.8; 3779025.8; 3779026.8; 3779027.8; 3779028.8; 3779029.8;
I want to know how I can make another parameter which calculates the cumulative difference between the values and presents it like this:
Elapsed Time:
0; 1; 2; 3; 4; 5; 6;
Thanks,

採用された回答

José-Luis
José-Luis 2012 年 8 月 16 日
Not entirely sure if that's what you meant but here goes:
data = sort(rand(10,1));
cumDiff = [0;cumsum(diff(data))];
Cheers!
  2 件のコメント
Friedrich
Friedrich 2012 年 8 月 16 日
編集済み: Friedrich 2012 年 8 月 16 日
Small note:
The perfomance of this pretty bad and it doesnt make any sense doing it like this. Simply subtract the first value from all others (assuming data is sorted ascending). This is approx. 20-30 times faster and give the exact same result.
>> data = (1:1000)';
>> tic,cumDiff = [0;cumsum(diff(data))];toc
Elapsed time is 0.000560 seconds.
>> tic,cumDiff = [0;cumsum(diff(data))];toc
Elapsed time is 0.000549 seconds.
>> tic,cumDiff = [0;cumsum(diff(data))];toc
Elapsed time is 0.000611 seconds.
>> tic,cumDiff = data-data(1);toc
Elapsed time is 0.000021 seconds.
>> tic,cumDiff = data-data(1);toc
Elapsed time is 0.000024 seconds.
>> tic,cumDiff = data-data(1);toc
Elapsed time is 0.000023 seconds.
>> tic,cumDiff = data-data(1);toc
Elapsed time is 0.000024 seconds.
José-Luis
José-Luis 2012 年 8 月 17 日
You are right. I guess i was blinded by the wording of the question.
Cheers!

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 16 日
編集済み: Azzi Abdelmalek 2012 年 8 月 16 日
A=[3779024.8; 3779025.8; 3779026.8; 3779027.8; 3779028.8; 3779029.8];
result=[0 ;cumsum(diff(A))];
disp('elapsed time');disp(sprintf('%d;',result))
  1 件のコメント
Aadil
Aadil 2012 年 8 月 16 日
Thanks,
I accept this answer as well

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by