Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Inverse of "diff" command

Subject: Inverse of "diff" command

From: FR

Date: 21 May, 2005 19:29:13

Message: 1 of 14

Hi All,

Can somebody give me a one liner command that does the opposite of
"diff" command (i.e. instead of subtracting the neighbouring elements
of an array, I would like to add them)?

Thanks in advance

FR

Subject: Inverse of

From: hatoum

Date: 21 May, 2005 22:05:08

Message: 2 of 14

FR wrote:
>
>
> Hi All,
>
> Can somebody give me a one liner command that does the opposite of
> "diff" command (i.e. instead of subtracting the neighbouring
> elements
> of an array, I would like to add them)?
>
> Thanks in advance
>
> FR

a(1:end-1)+a(2:end)

Subject: Inverse of "diff" command

From: tristram.scott@ntlworld.com (Tristram Scott)

Date: 23 May, 2005 09:07:20

Message: 3 of 14

FR <riaz_fahed@yahoo.com> wrote:
> Hi All,
>
> Can somebody give me a one liner command that does the opposite of
> "diff" command (i.e. instead of subtracting the neighbouring elements
> of an array, I would like to add them)?
>
> Thanks in advance
>
> FR

cumsum

--
Dr Tristram J. Scott
Energy Consultant
             

Subject: Inverse of "diff" command

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 23 May, 2005 11:33:37

Message: 4 of 14

In article <c3hke.14956$hn5.2689@newsfe2-win.ntli.net>,
tristram.scott@ntlworld.com (Tristram Scott) wrote:

> FR <riaz_fahed@yahoo.com> wrote:
> > Hi All,
> >
> > Can somebody give me a one liner command that does the opposite of
> > "diff" command (i.e. instead of subtracting the neighbouring elements
> > of an array, I would like to add them)?
> >
> > Thanks in advance
> >
> > FR
>
> cumsum
>
> Dr Tristram J. Scott
-------
  I don't think this is quite what FR wanted with "instead of subtracting
the neighbouring elements of an array, I would like to add them." Hatoum
has it right with "a(1:end-1)+a(2:end)" in "Re: Inverse of".

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford

Subject: Inverse of

From: Steve Amphlett

Date: 23 May, 2005 07:48:04

Message: 5 of 14

Roger Stafford wrote:
>
>
> In article <c3hke.14956$hn5.2689@newsfe2-win.ntli.net>,
> tristram.scott@ntlworld.com (Tristram Scott) wrote:
>
>> FR <riaz_fahed@yahoo.com> wrote:
>> > Hi All,
>> >
>> > Can somebody give me a one liner command that does the
opposite
> of
>> > "diff" command (i.e. instead of subtracting the
neighbouring
> elements
>> > of an array, I would like to add them)?
>> >
>> > Thanks in advance
>> >
>> > FR
>>
>> cumsum
>>
>> Dr Tristram J. Scott
> -------
> I don't think this is quite what FR wanted with "instead of
> subtracting
> the neighbouring elements of an array, I would like to add them."
> Hatoum
> has it right with "a(1:end-1)+a(2:end)" in "Re: Inverse of".

Maybe the OP will clarify?

I always find diff() a pain in the arse because it has no true
inverse (information is lost that can't be recovered). I usually end
up with this nasty expression [a(1); diff(a)], which can be cumsum'd
to give a again. Anyway, how about this for a more symmetrical pair
of inverses:

filter([1 -1],1,a) % [a(1); diff(a)]
filter([1 1],1,a) % Inverse of above

Subject: Inverse of

From: FR

Date: 23 May, 2005 14:32:16

Message: 6 of 14

Steve Amphlett wrote:
>
>
> Roger Stafford wrote:
>>
>>
>> In article <c3hke.14956$hn5.2689@newsfe2-win.ntli.net>,
>> tristram.scott@ntlworld.com (Tristram Scott) wrote:
>>
>>> FR <riaz_fahed@yahoo.com> wrote:
>>> > Hi All,
>>> >
>>> > Can somebody give me a one liner command that does the
> opposite
>> of
>>> > "diff" command (i.e. instead of subtracting the
> neighbouring
>> elements
>>> > of an array, I would like to add them)?
>>> >
>>> > Thanks in advance
>>> >
>>> > FR
>>>
>>> cumsum
>>>
>>> Dr Tristram J. Scott
>> -------
>> I don't think this is quite what FR wanted with "instead of
>> subtracting
>> the neighbouring elements of an array, I would like to add
them."
>> Hatoum
>> has it right with "a(1:end-1)+a(2:end)" in "Re: Inverse of".
>
> Maybe the OP will clarify?
>
> I always find diff() a pain in the arse because it has no true
> inverse (information is lost that can't be recovered). I usually
> end
> up with this nasty expression [a(1); diff(a)], which can be
> cumsum'd
> to give a again. Anyway, how about this for a more symmetrical
> pair
> of inverses:
>
> filter([1 -1],1,a) % [a(1); diff(a)]
> filter([1 1],1,a) % Inverse of above

Hi,

Dr. Hatoum's and Roger's reply is what I am looking for (i.e. sum up
the adjacent array elements as opposed to the difference of the
elements).

Thanks to everyone for their suggestions though.

FR

Subject: Inverse of

From: Yuji Zhang

Date: 18 Jun, 2013 03:36:09

Message: 7 of 14

Good point!

length(x) == length( diff(x) ) - 1
there's lost of info


"Steve Amphlett" <Firstname.Lastname@where_I_work.com> wrote in message <ef06b96.3@webx.raydaftYaTP>...
> Roger Stafford wrote:
> >
> >
> > In article <c3hke.14956$hn5.2689@newsfe2-win.ntli.net>,
> > tristram.scott@ntlworld.com (Tristram Scott) wrote:
> >
> >> FR <riaz_fahed@yahoo.com> wrote:
> >> > Hi All,
> >> >
> >> > Can somebody give me a one liner command that does the
> opposite
> > of
> >> > "diff" command (i.e. instead of subtracting the
> neighbouring
> > elements
> >> > of an array, I would like to add them)?
> >> >
> >> > Thanks in advance
> >> >
> >> > FR
> >>
> >> cumsum
> >>
> >> Dr Tristram J. Scott
> > -------
> > I don't think this is quite what FR wanted with "instead of
> > subtracting
> > the neighbouring elements of an array, I would like to add them."
> > Hatoum
> > has it right with "a(1:end-1)+a(2:end)" in "Re: Inverse of".
>
> Maybe the OP will clarify?
>
> I always find diff() a pain in the arse because it has no true
> inverse (information is lost that can't be recovered). I usually end
> up with this nasty expression [a(1); diff(a)], which can be cumsum'd
> to give a again. Anyway, how about this for a more symmetrical pair
> of inverses:
>
> filter([1 -1],1,a) % [a(1); diff(a)]
> filter([1 1],1,a) % Inverse of above

Subject: Inverse of

From: Yuji Zhang

Date: 18 Jun, 2013 03:46:11

Message: 8 of 14

I don't like diff either - it changes the length of the vector, making everything not convenient.

I've a further question here.
Consider a linear space context where
t = linear space;
dt = t(2)-t(1);
and y = y(t);

we have
dy = diff(y)/dt = gradient(y, dt)

so we can use
gradient(y, dt)*dt to replace diff(y)

is this a neat way? what's the inverse of it?
any thoughts are appreciated~






"Steve Amphlett" <Firstname.Lastname@where_I_work.com> wrote in message <ef06b96.3@webx.raydaftYaTP>...
> I always find diff() a pain in the arse because it has no true
> inverse (information is lost that can't be recovered). I usually end
> up with this nasty expression [a(1); diff(a)], which can be cumsum'd
> to give a again. Anyway, how about this for a more symmetrical pair
> of inverses:
>
> filter([1 -1],1,a) % [a(1); diff(a)]
> filter([1 1],1,a) % Inverse of above

Subject: Inverse of

From: Steven_Lord

Date: 18 Jun, 2013 14:06:45

Message: 9 of 14



"Yuji Zhang" <yuji.zhang@tufts.edu> wrote in message
news:kpokj9$6rr$1@newscl01ah.mathworks.com...
> Good point!
> length(x) == length( diff(x) ) - 1 there's lost of info

Yes, there's a loss of information. You cannot reproduce (*) the original
data set from the output of DIFF without knowing one of the elements and its
location in the original data set (preferably the first element; that makes
reconstruction very easy.) Without that extra information, you can reproduce
the original data set up to a constant offset.

x = 1:10;
dx = diff(x);
xMinusOne = cumsum([0 dx])
xExactly = cumsum([1 dx]) % the 1 is the extra bit of information

(*) reproduce == 'reproduce modulo round-off error, potential overflow or
underflow, saturation if you're using an integer data type, etc.'

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Inverse of

From: Yuji Zhang

Date: 18 Jun, 2013 19:45:13

Message: 10 of 14

Yea - nice explanation Steve! Do you have some comments about the "gradient" thing I mentioned earlier?

Any discussion's appreciated~


"Steven_Lord" <slord@mathworks.com> wrote in message <kppphl$t0t$1@newscl01ah.mathworks.com>...
>
>
> "Yuji Zhang" <yuji.zhang@tufts.edu> wrote in message
> news:kpokj9$6rr$1@newscl01ah.mathworks.com...
> > Good point!
> > length(x) == length( diff(x) ) - 1 there's lost of info
>
> Yes, there's a loss of information. You cannot reproduce (*) the original
> data set from the output of DIFF without knowing one of the elements and its
> location in the original data set (preferably the first element; that makes
> reconstruction very easy.) Without that extra information, you can reproduce
> the original data set up to a constant offset.
>
> x = 1:10;
> dx = diff(x);
> xMinusOne = cumsum([0 dx])
> xExactly = cumsum([1 dx]) % the 1 is the extra bit of information
>
> (*) reproduce == 'reproduce modulo round-off error, potential overflow or
> underflow, saturation if you're using an integer data type, etc.'
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Inverse of

From: Steven_Lord

Date: 19 Jun, 2013 14:16:52

Message: 11 of 14



"Yuji Zhang" <yuji.zhang@tufts.edu> wrote in message
news:kpqdc9$pto$1@newscl01ah.mathworks.com...
> Yea - nice explanation Steve! Do you have some comments about the
> "gradient" thing I mentioned earlier?

No, I haven't read it in depth.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Inverse of

From: Jan Simon

Date: 26 Jun, 2013 13:17:06

Message: 12 of 14

Dear Yuji Zhang,

I do like DIFF, because it performs it advertised work very efficiently: It replies the difference of neighboring elements. Of course this reduces the number of elements by 1, because this is the nature of differences.

I do not like GRADIENT very much, because it replies the expected values for evenly spaced data, but the speed is not impressing. For unevenly spaced values the 1st order method is not sufficient for my needs. Therefore I'm using the MEX DGradient: Up to 16 times faster, 2nd order method for unevenly spaced data.

> Consider a linear space context where
> t = linear space;
What does this exactly mean? Perhaps:
  t = linspace(0, 1, 1000)

Then "gradient(y, dt)*dt" is nor valid replacement of "diff(y)" due to the different number of elements. And it is not efficient in addtion. This would be much better:
  repmat(dt, size(y))

Kind regards, Jan

Subject: Inverse of

From: Yuji Zhang

Date: 28 Jun, 2013 06:08:06

Message: 13 of 14

Hi Jan~

Thanks for sharing.

I did mean "t = linspace(0, 1, 1000)". I was trying to writing some fake code by doing "t = linear space; "


I can imagine "diff" is slow and not exactly accurate. I was dealing with some simple smooth curve. So I found the reverse pair "diff" <-> "cumtrapz" was quite repeatable for my precision.

Does
>repmat(dt, size(y))
just mean
[dt dt dt dt dt dt ...] ? What does it have to do with our topic?

Let me know. Thanks man~

yuji



"Jan Simon" wrote in message <kqepki$suf$1@newscl01ah.mathworks.com>...
> Dear Yuji Zhang,
>
> I do like DIFF, because it performs it advertised work very efficiently: It replies the difference of neighboring elements. Of course this reduces the number of elements by 1, because this is the nature of differences.
>
> I do not like GRADIENT very much, because it replies the expected values for evenly spaced data, but the speed is not impressing. For unevenly spaced values the 1st order method is not sufficient for my needs. Therefore I'm using the MEX DGradient: Up to 16 times faster, 2nd order method for unevenly spaced data.
>
> > Consider a linear space context where
> > t = linear space;
> What does this exactly mean? Perhaps:
> t = linspace(0, 1, 1000)
>
> Then "gradient(y, dt)*dt" is nor valid replacement of "diff(y)" due to the different number of elements. And it is not efficient in addtion. This would be much better:
> repmat(dt, size(y))
>
> Kind regards, Jan

Subject: Inverse of

From: Jan Simon

Date: 28 Jun, 2013 10:00:14

Message: 14 of 14

Dear Yuji,

DIFF is really accurate for its purpose: It calculates the difference between neigboring elements. DIFF is not accurate for calculating the derivative, because this is a 1st-order approximation only, while GRADIENT offers a 2nd order method.

The difference of neighboring elements of a LINSPACE vector is constant, because this is the definition of a vector of evenly spaced elements.
Then x(2)-x(1) is exactly the same as x(3)-x(2), expcept for rounding due to the limited precision.
In consequence this creates a repeated constant, which can be created much faster by REPMAT:

  diff(linspace(0,1,17))

Is this clearer now?

Kind regards, Jan

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us