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 Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by