Find the difference between non-zero elements

I am trying to determine the differences between the non zero values in the data attached. I have achieved this using the code below
xdiff = diff(xdup(xdup~=0));
This provides a new array of size (27 x 1 double)
However I would like to map the new delta values to the original array (xdup)by including the original zero values. I understand the new array will be (47 x 1 double) compared to xdup (48 x 1 double. Please see xdiff in the data set as an example of what I want to achieve.
I hope this makes sense?

4 comentarios

Image Analyst
Image Analyst el 22 de Mzo. de 2014
What do you mean by "map"?
And for each pair of elements, the kth and the (k+1)st, which of those two elements do you want to be associated with the difference between them, the upper one or the lower one? Realize that if xdiff(k) is associated with xdup(k) and xdup(k+1) then it cannot be associated with the difference between xdup(k-1) and xdup(k). You'd need two difference arrays for that.
Steven Ross
Steven Ross el 22 de Mzo. de 2014
Editada: Steven Ross el 22 de Mzo. de 2014
OK sorry for the confusion I understand about having a separate array for xdiff. I haven't explained the problem very well. When I use the line of code xdiff = diff(xdup(xdup~=0)); An array 27 elements long is returned the problem I have then is applying the correct time data to the difference values. I want the kth element to be associated with the first difference value. But I also want the "unwanted zeros" included in xdiff as in the example data set in xdiff (47 elements) and then I can take out the zeros at a later time after I have created a matrix that includes both xdiff and time data columns. As it is the data for xdiff is returned as shown in xdiff non zero.xlsx attached and I cant map it to the time data.
Jan
Jan el 22 de Mzo. de 2014
It would be useful, if you offer the data in a more convenient way than an XLSX file.
Steven Ross
Steven Ross el 22 de Mzo. de 2014
Editada: Jan el 22 de Mzo. de 2014
Original data [EDITED, Jan, this allows a copy&paste now:]
xdup = [1.85E-05
0
0
0
0
0
0
0
0
0
0
1.82E-05
0
1.79E-05
0
1.76E-05
1.74E-05
0
1.71E-05
1.68E-05
0
1.65E-05
1.62E-05
1.59E-05
1.56E-05
1.53E-05
1.50E-05
1.44E-05
1.41E-05
1.35E-05
1.32E-05
1.26E-05
1.24E-05
1.18E-05
1.15E-05
1.12E-05
1.06E-05
1.03E-05
0
1.00E-05
9.71E-06
0
9.41E-06
0
0
0
0
9.12E-06]
How the data is returned
xdiff no zeros -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -5.88E-07 -2.94E-07 -5.88E-07 -2.94E-07 -5.88E-07 -2.94E-07 -5.88E-07 -2.94E-07 -2.94E-07 -5.88E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07
How I would like to have the data
xdiff -2.94E-07 0 0 0 0 0 0 0 0 0 0 -2.94E-07 0 -2.94E-07 0 -2.94E-07 -2.94E-07 0 -2.94E-07 -2.94E-07 0 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -2.94E-07 -5.88E-07 -2.94E-07 -5.88E-07 -2.94E-07 -5.88E-07 -2.94E-07 -5.88E-07 -2.94E-07 -2.94E-07 -5.88E-07 -2.94E-07 -2.94E-07 0 -2.94E-07 -2.94E-07 0 -2.94E-07 0 0 0 0

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 22 de Mzo. de 2014
Editada: Jan el 22 de Mzo. de 2014
What about this:
index = (xdup ~= 0);
result(index) = [diff(xdup(index)); 0];
Now you have to crop the last element of the result.

Categorías

Etiquetas

Preguntada:

el 22 de Mzo. de 2014

Editada:

Jan
el 22 de Mzo. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by