这种方法怎么样?
% your data
F=[0 ; 0; 0; 0; 0; 0 ; 1; -79; -84; 35 ; 75; 0 ;0];
% growth state
growthstate = [0; sign(diff(F))];
% state relative to the lim1
% true if crossing lim1
lim1=30;
lim1state_p = [false; diff(F-lim1>0)];
lim1state_n = [false; diff(F+lim1>0)];
lim1state = lim1state_p | lim1state_n
% the same for lim2
% true if crossing lim2
lim2=40;
lim2state_p = [false; diff(F-lim2>0)];
lim2state_n = [false; diff(F+lim2>0)];
lim2state = lim2state_p | lim2state_n;
% decod growth state
s{1}='GoingDown';
s{2}='GoingUp';
decode = @(idx) s{double(idx>0) + 1};
c = {};
for k=1:length(F)
if lim1state(k)
c(end+1,:) = {F(k-1), F(k), k, lim1, decode(growthstate(k))};
end
if lim2state(k)
c(end+1,:) = {F(k-1), F(k), k, lim2, decode(growthstate(k))};
end
end
% show
c
我注释了程序的输出。
| F(k-1) | F(k) | k | level | | reason |
|--------+------+----+-------+-----------+------------------------|
| 1 | -79 | 8 | 30 | GoingDown | cross -30,F(k)<F(k-1) |
| 1 | -79 | 8 | 40 | GoingDown | cross -40,F(k)<F(k-1) |
| -79 | -84 | 9 | - | - | no crossing |
| -84 | 35 | 10 | 30 | GoingUp | cross -30, F(k)>F(k-1) |
| -84 | 35 | 10 | 40 | GoingUp | cross -40, F(k)>F(k-1) |
| 35 | 75 | 11 | 40 | GoingUp | cross 40, F(k)>F(k-1) |
| 75 | 0 | 12 | 30 | GoingDown | cross 30, F(k)<F(k-1) |
| 75 | 0 | 12 | 40 | GoingDown | cross 40, F(k)<F(k-1) |