Subset of time series of fixed length?

1 次查看(过去 30 天)
How do I search for the indices in a vector for a subset of a fixed length? Say, I have a vector of length 100. The average values of the indices between 87 to 91 (fixed length 5) is less than 0.5, which is the desired condition to find the subset. Is there any easy way to search over this hundred length vector other than using a loop and fixed window of length 5?

回答(2 个)

dpb
dpb 2014-6-12
编辑:dpb 2014-6-12
...the average values of the indices between 87 to 91 (fixed length 5) is less than 0.5...
I'll presume you mean the average value of the contents of the vector between the two points.
idx=5*(find(mean(reshape(v,5,[]))<0.5)-1).'+1;
will be the beginning of the group if you're content to use a non-sliding window of five.
If it's a requirement to use the sliding window,
f=filter(0.2*ones(1,5),1,v);
will give the mean vector to search over. Of course, it has four values at the beginning that are startup values that you'll want to discard.
ADDENDUM
Example for the last...
>> v=rand(20,1);
>> f=filter(0.2*ones(1,5),1,v);
>> m=[];for i=1:16,m(i)=mean(v(i:i+4));end
>> [f [zeros(4,1); m.']]
ans =
0.1298 0
0.2762 0
0.4057 0
0.4959 0
0.6053 0.6053
0.5347 0.5347
0.5373 0.5373
0.4456 0.4456
...
0.6384 0.6384
0.6106 0.6106
0.5418 0.5418
0.6149 0.6149
0.4903 0.4903
>>
I abbreviated the output manually...
  2 个评论
Adib Zaman
Adib Zaman 2014-6-13
Thanks so much! This is really great to use the filer function without using a loop. In my case, this has to be sliding window.
However, I was not looking for the exact same thing. I wanted to change the search criteria to different conditions. Say, we have a vector x and we want to find the indices of the subset for which the length is 3, and the condition is that 2/3rd of the data, i.e. 66% of them have to smaller than 4. For example, if x is, length is 3
>> x=[2 3 4 6 7 8 9 2 1 3 4]
x =
2 3 4 6 7 8 9 2 1 3 4
>> length = 3
length =
3
with the above condition, I want my function my_find_func(x,condition,length) to return the indices [1 7 8 9] cause at those indices x has four subsets [2 3 4],[9 2 1],[2 1 3] and [1 3 4] for which the lengths are 3 and 2 out of 3 numbers are less than 4.
Adib Zaman
Adib Zaman 2014-6-13
Here is an exact code that uses for loop. The goal is to find only the sliding windows of length 5 for which the 90% of the numbers are between 10 and 200.
clc
clear all
close all
mu = 100;
sigma = 50;
x = round(normrnd(mu,sigma,1,10))
sliding_window_size = 5;
for index = 1:length(x)-sliding_window_size+1
current_subset = x(index:index+sliding_window_size-1)
fifth_prctile = prctile(current_subset,5)
ninety5th_perctile = prctile(current_subset,95)
if(prctile(current_subset,5)>10 & prctile(current_subset,95)<200)
index
current_subset
disp('Selected')
else
% disp('Not selected')
end
end

请先登录,再进行评论。


José-Luis
José-Luis 2014-6-13
编辑:José-Luis 2014-6-13
myTest = @(x) prctile(x,5)>10 & prctile(x,95) < 200;
test_mat = hankel(1:10,1:10);
test_mat = test_mat(1:sliding_window_size, 1 : numel(x) - sliding_window_size + 1);
myTest(x(test_mat));
Further gains can be obtained if you can figure out a way of not calling prctile() twice. Maybe using quantile() instead as it accepts multiple values as input.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by