![Find the location of the change - 2019 01 25.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/201542/Find%20the%20location%20of%20the%20change%20-%202019%2001%2025.png)
Find the location of the change
조회 수: 45 (최근 30일)
이전 댓글 표시
Hi :)
I would like to find the location of the change in an array. The arrays are ramp and hold, i e. they contain several identical values then start to increase and then become constant again. The increase rates are different in each array.They are plotted below:
![Capture.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200674/Capture.jpeg)
I want to plot a vertical line at the end and begining of each constant section. Can you help me with that?
Thanks
댓글 수: 0
채택된 답변
Star Strider
2019년 1월 13일
편집: Star Strider
2019년 1월 25일
If you have the Signal Processing Toolbox, consider using the findchangepts (link) function, with the 'Statistic','mean' name-value pair. (The findchangepts function was introduced in R2016a.)
EDIT — (25 Jan 2019 at 20:37 UCT)
Thank you for attaching your data.
The core MATLAB ischange (link) function (introduced in R2017b) turns out to be the best option for this.
The Code —
D = load('XX.mat');
XX = D.XX;
t = linspace(0, numel(XX), numel(XX));
[TF,SegMean,SegVar] = ischange(XX,'linear','Threshold',500);
cpts = find(TF);
figure
plot(t, XX)
hold on
plot(t(cpts), XX(cpts), '^r', 'MarkerFaceColor','r')
hold off
The Plot —
![Find the location of the change - 2019 01 25.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/201542/Find%20the%20location%20of%20the%20change%20-%202019%2001%2025.png)
댓글 수: 0
추가 답변 (1개)
Image Analyst
2019년 1월 13일
Use diff() and line()
Something like (untested)
dy = 0, diff(y)];
% Get non-zero dy
mask = dy~= 0;
dy = dy(mask);
xx = x(mask); % Get x values at those locations.
hold on;
for k = 1 : length(dy)
thisX = xx(k); % Find the x location.
line([thisX, thisX], ylim, 'Color', 'r', 'LineWidth', 2); % Draw a vertical red line.
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!