Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Cumulative Difference Vectorization?
Replies: 1   Last Post: May 13, 2013 4:52 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View  
Blake Fleischer

Posts: 2
Registered: 4/17/12
Re: Cumulative Difference Vectorization?
Posted: May 13, 2013 4:52 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

So, maybe this is just me, but I didn't find this to work - eg:
a = [1;3;5;8;12];
b = filter(1,[1 1],a)

This yields:
b =

1
2
3
5
7

The a cumulative difference should be the opposite of the cumsum - so one would expect the cum difference to be:
b =
1
2
2
3
4

such that cumsum(b) yields:
ans =

1
3
5
8
12

The reason an infinite filter wouldn't work is that it uses the previous output in the next input. If we only care about the difference between values (opposite of cumsum) then an IIR filter wouldn't yield the expected result.

The proper way to vectorize a 'cumdiff' is deceptively simple: just subtract the vector from itself:

b = a(2:end)-a(1:end-1);
b = [a(1); b] %append the first value so the cumsum will work

or in one line:
b = [a(1); a(2:end)-a(1:end-1)];



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.