create a sliding window to determine when a % of numbers in a sequence equal a certain value.

4 vues (au cours des 30 derniers jours)
Hello! I am new to Matlab, thank you in advance for helping me. I have a serie (called S) of around 800 numbers (or more) composed of values 1,2 or 4 (for ex. 111122111142222211 etc). In this series, I would like to find out when for every 100 numbers for ex. (this number can vary) more than/or equal to 80% (the % can vary) of the numbers have the value 2. Is it possible to create something like a sliding window which would slide through the serie S (beginning by 1-100, then 2-101, then 3-102 something like that) and tell me at what point >80% of the numbers have value 2? Thank you very much in advance, and feel free to ask me any questions!
  1 commentaire
Azzi Abdelmalek
Azzi Abdelmalek le 25 Avr 2016
This is not clear, you can explain your problem with a short example,and post the expected result.

Connectez-vous pour commenter.

Réponses (1)

David Young
David Young le 25 Avr 2016
The answer to problems involving a sliding window is very often to use the convolution operation, as here.
% some random test data, with 1s, 2s and 4s (mainly 2s)
N = 800; % length of series
S = ones(1, N);
r = rand(1, N);
S(r < 0.75) = 2;
S(r > 0.9) = 4;
fprintf('First 20 values: '); fprintf('%d ', S(1:20)); fprintf('\n');
% Now the computation to find where a window of 100 has more than a
% certain fraction of twos
% parameters for the search
windowLength = 100;
testPercent = 80;
% find which of the values are 2
twoPositions = S == 2;
% count the number of twos in each window of length 100
twoSums = conv(double(twoPositions), ones(1, windowLength), 'valid');
% find the positions where the threshold is exceeded
manyTwos = find(twoSums >= windowLength * testPercent / 100);
% Print out the positions of the start of the window where
% there are the required number of twos
fprintf('Positions with many twos: '); fprintf('%d ', manyTwos); fprintf('\n');
  1 commentaire
Anastasia
Anastasia le 26 Avr 2016
Thank you very much for your response. So, in my program, I only use this part, right?
% Now the computation to find where a window of 100 has more than a % certain fraction of twos % parameters for the search windowLength = 100; testPercent = 80; % find which of the values are 2 twoPositions = S == 2; % count the number of twos in each window of length 100 twoSums = conv(double(twoPositions), ones(1, windowLength), 'valid'); % find the positions where the threshold is exceeded manyTwos = find(twoSums >= windowLength * testPercent / 100); % Print out the positions of the start of the window where % there are the required number of twos fprintf('Positions with many twos: '); fprintf('%d ', manyTwos); fprintf('\n');
It seems to be working well, i should test some more :)
However, I was wondering if it is possible to plot the result rather than print out the positions of the start of the window? Thank you again!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming 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