finding sequences
9 views (last 30 days)
Show older comments
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 Comment
Doug Hull
on 27 Oct 2011
You are going to need to describe the goal in more detail than this. Give an example input and expected output.
Answers (3)
Walter Roberson
on 27 Oct 2011
sign(diff()) will be positive for the duration of each increasing sequence, and negative for the duration of each decreasing sequence.
0 Comments
Dr. Seis
on 27 Oct 2011
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 Comments
See Also
Categories
Find more on Numeric Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!