Calculations on every 2nd row in a matrix in sets of 2 rows

1 view (last 30 days)
I have a matrix of x and y points with 2 columns and n number of rows.
The first column is the x data (distance) and the 2nd coloumn is the y data (height (or depth in this case as they are underwater)). Each row represents a specific point on a topographic profile relating to either the top or bottom of a scarp so every two rows define the top and bottom of a specific scarp i.e., row 1 represents the top of a scarp and row 2 represents the bottom. Row 3 represents the top of a another scarp and row 4 represents the bottom of this other scarp, and so on. I want to calcualte the slope and height for each scarp. Therefore, I need to calculate the change in x and the change in y for each set of 2 rows representing the top and bottom of each scarp. I can't figure out how. Perhaps I need a loop?
An example of the data is as follows:
x (distance) y (Depth)
3704.43613041155 -1705.01061571125
3839.12346338856 -1751.88959660297
3973.81079636558 -1748.49256900212
4063.60235168359 -1762.76008492569
4211.11704970604 -1738.98089171975
4390.70016034206 -1787.89808917197
4814.00320684126 -1749.17197452229
4852.48530197755 -1766.15711252654
5570.81774452165 -1684.62845010616
5788.88295029396 -1742.37791932059
6526.45644040620 -1662.88747346072
6558.52485301978 -1683.26963906582
Many thanks in advance

Accepted Answer

Ameer Hamza
Ameer Hamza on 16 Sep 2020
Something like this
f = fopen('data.txt');
data = textscan(f, '%f %f', 'HeaderLines', 1);
fclose(f);
data = [data{:}];
data_part = mat2cell(data, 2*ones(size(data,1)/2,1), 2);
data_diff = cellfun(@(x) diff(x), data_part, 'uni', 0);
slopes = cellfun(@(x) x(2)/x(1), data_diff);
heights = cellfun(@(x) x(2), data_diff);
(data.txt is attached)

More Answers (1)

madhan ravi
madhan ravi on 16 Sep 2020
matrix(1 : 2 : end, :) - matrix(2 : 2 : end, :) % ?

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!