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

조회 수: 1 (최근 30일)
Alex Hughes
Alex Hughes 2020년 9월 16일
댓글: Alex Hughes 2020년 9월 16일
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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 9월 16일
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)

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 9월 16일
matrix(1 : 2 : end, :) - matrix(2 : 2 : end, :) % ?

Community Treasure Hunt

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

Start Hunting!

Translated by