finding sequences

조회 수: 9 (최근 30일)
Robert
Robert 2011년 10월 27일
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
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
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.

Dr. Seis
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).

Robert
Robert 2011년 10월 27일
thanks alot, you're a life saver

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by