finding sequences
9 次查看(过去 30 天)
显示 更早的评论
I am trying to find sequences of increasing or decreasing values from a set of data.
For each sequence I will have to find the length of the sequence and the slope for each sequence.
1 个评论
Doug Hull
2011-10-27
You are going to need to describe the goal in more detail than this. Give an example input and expected output.
回答(3 个)
Walter Roberson
2011-10-27
sign(diff()) will be positive for the duration of each increasing sequence, and negative for the duration of each decreasing sequence.
0 个评论
Dr. Seis
2011-10-27
Here is an example that will get you on your way:
t = 0:0.1:10;
f = sin(2*pi/3*t);
p = zeros(size(t));
s = ones(size(t));
num_seq = 1;
if f(2) - f(1) > 0
inc_or_dec = 'inc';
p(1:2) = 1;
else
inc_or_dec = 'dec';
p(1:2) = -1;
end
for i = 3 : length(t)
if f(i) - f(i-1) > 0
if isequal(inc_or_dec,'dec')
inc_or_dec = 'inc';
num_seq = num_seq+1;
end
p(i) = 1;
else
if isequal(inc_or_dec,'inc')
inc_or_dec = 'dec';
num_seq = num_seq+1;
end
p(i) = -1;
end
s(i) = num_seq;
end
figure;
subplot(2,1,1);
plot(t,f,'b-',t(p>0),f(p>0),'go',t(p<0),f(p<0),'ro');
legend('data','increasing data','decreasing data');
subplot(2,1,2);
plot(t,s,'b-');
legend('sequence number'); ylim([0 num_seq+1]);
It basically checks each sample in your data ("f" in my example) to see if it is increasing or decreasing. Each time it detects a change from "inc" to "dec" (or "dec" to "inc") it groups the subsequent data points in a different sequence. The values in "s" denote which sequence those data points belong to. I will leave it up to you to figure out how to determine the length of the sequence and the slope (hint: look at the way I plot points that follow an increasing and decreasing trend to select data points within a specific sequence).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!