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 个评论

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.
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.
It would be useful, if you offer the data in a more convenient way than an XLSX file.
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

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2014-3-22
编辑:Jan 2014-3-22
What about this:
index = (xdup ~= 0);
result(index) = [diff(xdup(index)); 0];
Now you have to crop the last element of the result.

类别

帮助中心File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

编辑:

Jan
2014-3-22

Community Treasure Hunt

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

Start Hunting!

Translated by