Effacer les filtres
Effacer les filtres

Subset of time series of fixed length?

1 vue (au cours des 30 derniers jours)
Adib Zaman
Adib Zaman le 12 Juin 2014
Modifié(e) : José-Luis le 13 Juin 2014
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?

Réponses (2)

dpb
dpb le 12 Juin 2014
Modifié(e) : dpb le 12 Juin 2014
...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 commentaires
Adib Zaman
Adib Zaman le 13 Juin 2014
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 le 13 Juin 2014
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

Connectez-vous pour commenter.


José-Luis
José-Luis le 13 Juin 2014
Modifié(e) : José-Luis le 13 Juin 2014
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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by