Reputation: 11
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
Upvotes: 1
Views: 110
Reputation: 45752
I'm not sure exactly what output you're after but you can find a group of 10 or more 1
s quite easily using convolution:
w = [0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1]
n = 10;
find(conv(w, ones(1,n)) == n) - n + 1
this gives you the indices of where groups of 10 start
Upvotes: 2
Reputation: 221534
Solution based on strfind
to find starting indices of N or N+ consecutive ones -
%// sample accessibility vector (different from the one in question)
access_vec = [1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0]
N = 10; %// N or N+ consecutive ones are to be detected
indices = strfind([0 access_vec],[0 ones(1,N)])
Output -
indices =
1 13
Another example -
access_vec = [ 0 1 zeros(1,10) 1]
Output -
indices =
[]
Upvotes: 1
Reputation: 112659
Let the data be
w = [0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1];
step_index = 101;
step_duration = 10;
Then:
d = diff([0 w 0]);
starts = find(d==1); %// indices of starts of runs of 1's
ends = find(d==-1); %// indices of ends of runs of 1's (actually starts of 0's)
keep = find(ends-starts>=step_duration); %// detect runs that are long enough
starts = starts(keep)+step_index-1; %// keep only those runs and adjust with "-1"
ends = ends(keep)+step_index-2; %// "-2" because of how ends is defined
The result is in vectors starts
and ends
. In your example there is just one such window, so they contain just one element:
starts =
109
ends =
118
Upvotes: 2
Reputation: 21563
I think you are looking for a period with good weather, where this would be defined as having sufficient ones in a stretch of 10.
In that case you could use a filter to check how many ones occur in each stretch of 10. It could be something like this:
f= filter(ones(10,1),1,x)
Not sure about the syntax, but check it out. Filter should definitely get you there.
You could then follow up by finding good periods like so:
find(f==10)
find(f>=8)
find(f==max(f))
Upvotes: 0