Subtracting one row from a previous row in a single column of n long data

I have a single column of data that is n long and I want to be able to subtract the second row in that column by the first and have that pattern repeat. (third row subtracted by the second, fourth row subtracted by the thrid and so on).
Ex:
1
5
6
7
4
3
I would like to be able to have a code that does 5-1, 6-5, 7-6, 4-7, 4-3.
I am pretty new to MatLab so if I am using incorrect terminology or my question is confusing in any way please let me know.

 Accepted Answer

A = randi(9, 8, 2) %some data to illustrate
A = 8×2
1 4 9 2 9 7 6 3 1 9 5 7 5 8 7 8
diff(A, [], 1) %subtract row by row
ans = 7×2
8 -2 0 5 -3 -4 -5 6 4 -2 0 1 2 0

More Answers (1)

Use the diff function
a = [1
5
6
7
4
3];
diff(a)
ans = 5×1
4 1 1 -3 -1

7 Comments

I attempted this but it continues to give me an error that reads: Arrays have incompatible sizes for this operation. Any ideas?
That error's not coming from diff() but probably a subsequent line and is due to the fact that the output from diff() is an array that is smaller than the input to diff(), in this case smaller by 1.
Caution: when you use diff() with only one parameter, then diff() is taken along the first non-singular dimension, which might not be along the columns.
A = magic(5);
for K = [3 2 1]
B = A(1:K,:)
diff(B)
end
B = 3×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22
ans = 2×5
6 -19 6 6 1 -19 1 6 6 6
B = 2×5
17 24 1 8 15 23 5 7 14 16
ans = 1×5
6 -19 6 6 1
B = 1×5
17 24 1 8 15
ans = 1×4
7 -23 7 7
Notice that ans has 5 columns for K = 3 and K = 2, but suddenly has 4 columns for K = 1 ? Because for K = 1, the first non-singular dimension of B is the second dimension.
As you had distinctly requested to take differences between rows, in my Answer I showed the form that takes differences between rows even if there is only one row: diff(A, [], 1)
depth = xlsread('U1329 Calculated data sheet.xlsx', 'Sheet1', 'A:A');
bulkdensity = xlsread('U1329 Raw data set.xlsx', 'Sheet1', 'L:L');
graindensity = xlsread('U1329 Raw data set.xlsx', 'Sheet1', 'N:N');
porosity = xlsread('U1329 Raw data set.xlsx', 'Sheet1', 'O:O');
voidratio = xlsread('U1329 Raw data set.xlsx', 'Sheet1', 'P:P');
data = [depth bulkdensity graindensity porosity voidratio];
N = diff(depth,[],1);
a = (((bulkdensity - 1.024)*9.81).*N)/1000;
This is what I have for my code and I am still getting the same error. I think I need to change something about the a formula but im not sure what. Any ideas?
Like I said, "due to the fact that the output from diff() is an array that is smaller than the input to diff(), in this case smaller by 1."
So, say bulkdensity and depth are each of size 100-by-1, then N will be of size 99-by-1, so you can't do an element-wise operation (.*) on a vector with 100 elements and a vector with 99 elements. Or, restated in terms of what your data represent: if you have data at certain depths, say 100 of them, then you'll get 99 depth intervals between those depths, so something's gotta give...
You have to decide whether each bulkdensity data point should be associated to the top of its depth interval (and use bulkdensity(1:end-1) in your calculation) or the bottom (use bulkdensity(2:end)). In either case you have to discard a bulkdensity sample because there are 1 more bulkdensity values than there are depth intervals.
This type of thing comes up a lot, so there's a name for it: https://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error
Okay, i'll give that a shot. Thanks for the explanation.
It is common that gradient() should be used instead of diff()

Sign in to comment.

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!