Finding average slope without losing steep slopes
32 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi everyone, I am plotting river profiles with distance upstream along the x and elevation/height along the y. I am then wanting to take the average slope change within the river profile; however, many of the profiles I am plotting include points where distance remains the same but elevation increases (see image below for an example), leading to slope values of infinity. I previously removed these infinity values to calculate average slope but realized by removing them, I'm skewing the average slope towards the lower slope values, which impacts my end goal. Does anyone have any suggestions on how I can calculate average slope while also accounting for these steep slopes.
Example of data:
ch_height =
Columns 1 through 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
ch_length =
Columns 1 through 20
0 0 0 0 0.4612 0.9225 0.9225 0.9225 1.5748 2.2271 3.5317 3.5317 3.5317 3.9929 3.9929 4.4542 5.5677 5.5677 5.5677 5.5677
Plotted Profile (not concerned about data below sea-level in this example):
Portion of Code related to question:
slope=diff(ch_height)./diff(ch_len);
slope=slope(slope~=0 & isfinite(slope)); % this is where I previously removed infinity values for the steep slopes
I was thinking of possibly connecting the last point of the infinie slope to the previous point where the ch_len is of a different value. For example, where ch_len = 0, connecting the point (0,0) with the point (0.4612, 4). If this is the best option, how could I code this so that it applies to each of the infinite slopes?
Thanks in advance!
0 comentarios
Respuestas (1)
Star Strider
el 3 de Oct. de 2020
I have no idea whatt you actually want to do. One option is to simply do a linear regression on the entire data:
ch_height = [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19];
ch_length = [0 0 0 0 0.4612 0.9225 0.9225 0.9225 1.5748 2.2271 3.5317 3.5317 3.5317 3.9929 3.9929 4.4542 5.5677 5.5677 5.5677 5.5677];
B = [ch_length(:) ones(size(ch_length(:)))] \ ch_height(:);
Slope = B(1)
Intercept = B(2)
X_Intercept = -B(2)/B(1)
figure
plot(ch_length, ch_height, '-p')
hold on
plot(ch_length, [ch_length(:) ones(size(ch_length(:)))]*B, '--r')
hold off
grid
xlabel('Length')
ylabel('Height')
legend('Data','Linear Regression', 'Location','SE')
.
2 comentarios
Star Strider
el 3 de Oct. de 2020
This is the best I can come up with:
[TF,m,b] = ischange(ch_length,'linear', 'Threshold',0.001); % Change Points, Slope At Every Point
[um,~,idx] = unique(m, 'stable'); % Unique Slope Values
meanm = accumarray(idx, (1:numel(idx)).', [], @(x)mean(m(x))); % Means Of Unique Slope Values
meanm = meanm(meanm>1e-3) % Means Of Unique Slope Values (Omitting ‘Steps’)
The ischange function calculates the slopes and intercepts at every point of the data, then the accumarray call averages those that unique identifies as being the same. (You could also use uniquetol, however uniquetol sorts the outputs iin ascending order. Specifying 'stable' with unique — not an option with uniquetol — preserves the original ordering.)
Here, it identified and calculated:
meanm =
0.4612
0.6523
1.1135
With different or more extensive data, it can likely do more accurate calculations.
Ver también
Categorías
Más información sobre Scatter Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!