Tak
Tak

Reputation: 3616

Results not correct when using diff function in Matlab

I'm using the diff Matlab function to get the difference between two successive values. And as shown here in this vector nz in this link as shown in nz the difference between col 261 and 260 is -1342 but when I use this script the result of difference between this coloumns don't appear in the result dnz. So if anyone could advise why this is not working? This is my attempt:

load('nz.mat');
dnz = diff(nz);

Upvotes: 0

Views: 196

Answers (1)

Mohsen Nosratinia
Mohsen Nosratinia

Reputation: 9864

If you type class(nz) you see that your data is unit16. And MATLAB saturates the results when dealing with integer values, i.e. since 0 - 1342 is lower than zero (the smallest value in uint16) it returns zero:

>> dnz=diff(nz);
>> dnz(260)

ans =

      0

If you convert it to a class that can accomodate -1342 like int16 you get

>> dnz = diff(int16(nz));
>> dnz(260)

ans =

  -1342

Upvotes: 4

Related Questions