How to find a trend in a sequence?
1 view (last 30 days)
Show older comments
I have an accessibility vector with 1's and 0's. 1 means the weather is good; and 0 meaning the weather is not good and the place is not accessible.
I have a step_duration of (e.g.) 10 hrs. Considering the step_index (start of the step) as 101,I need to find a window of straight 10 hrs of good weather.
Expected solution: With 10 hours of expected weather, say the accessibility vector is [0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1] . So, the indexes that we find the window are from 109-118. And the corresponding weather delay (considering we could not find straight hours) is from index 101-108 (i.e. 9 hours.) I need to write a code for such an algorithm.
Some sample code that I could think of is as follows( though this is not exactly what I want):
window = 0; % Initialize the counter for finding the weather window.
for tt = step_index
if Accessibility(tt) == 0
% bad weather, move to next index
% reset window.
window = 0;
k = k + 1;
possible_window(k) = window;
continue
else
% Increase window
window = window + 1;
% sote window history
k = k + 1;
possible_window (k) = window;
end
end
tt = tt + 1;
end
if true
% code
end
2 Comments
Answers (2)
Star Strider
on 11 Sep 2014
You can use the filter function (here, ‘av’ is the accessibility vector):
av = randi(2, 1, 100)-1; % Create Data
av(20:29) = ones(1,10); % Insert Series of 10 1’s
y = filter(ones(1,10), 10, av);
yf = find(y > 0.99);
The ‘yf’ variable will find the end index of the sequences, and the size of ‘yf’ will be the number of sequences in a given accessibility vector.
4 Comments
Star Strider
on 11 Sep 2014
Edited: Star Strider
on 11 Sep 2014
My code finds the end-indices of your ‘step_duration’ as I understand it. To find the beginnings, count back 10. (Also, if there are 11 consecutive 1s, it will return 2 indices, if 12, 3 indices, and so on.) I don’t understand how ‘step_index’ enters into it, since it wasn’t part of the ‘accessibility vector’ you posted.
If you have fewer than 10 consecutive 1s, it will generate values <1. If you have other criteria for the step_duration — fewer or more than 10 consecutive 1s — change the filter coefficients appropriately, with the ‘b’ vector a ones vector of appropriate length, and changing the ‘a’ vector (the 10 in my code) to the length of the ones vector.
If you want to find the occurrences of ‘[1 0 1]’, use the same filter idea with a different pattern:
av = randi(2, 1, 100)-1;
si = filter([1 0 1], 2, av);
sidx = find(si > 0.99);
The ‘si’ vector is 1 where the the filter detects the end of a ‘[1 0 1]’ sequence. The ‘b’ value has to be the sum of the elements of the ‘a’ vector.
I don’t understand the algorithm you need to write, but my code gets you very close. It can detect the ‘step_index’ with the ‘sidx’ variable, and detect the ‘step_duration’ in the ‘yf’ variable as you defined it in your Question, and can give you the indices for both. The filter calls will take your entire 87600-element vector and produce the indices for the required patterns. It seems to me all you then need to do is program the logic relating to the ‘step_index’ and ‘step_duration’ indices to do what you want.
See Also
Categories
Find more on Digital Filter Analysis 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!