Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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

 Blake Fleischer Posts: 2 Registered: 4/17/12
Re: Cumulative Difference Vectorization?
Posted: May 13, 2013 4:52 PM

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)];