Finding a minumum range of sequential values

3 vues (au cours des 30 derniers jours)
bugguts99
bugguts99 le 22 Juin 2011
Further to a question that was asked a few days ago (which Matt Fig answered to the satisfaction of the author - see link below):
In particular, I am searching for a sequence of a minimum of 5 ones in order to perform another function on these elements. That is to say that I would like to perform the second function on any sequence where 5 or more ones may occur. Currently my code looks like:
S3 = findstr(B, ones(1, 5));
But when performing the second action, should the sequence happen to be greater than 5, I find that only the first 5 values identified with the code above are addressed. For example, I have a run of 10 ones, where only the first five values are are found and subsequently computed. Instead, I would like to compute values for all 10 elements appearing in the sequence.
Any ideas on how I might be able to get around this?
The data is a single row vector (1 X 14620); A range of integers where min value = 1 and max value = 6578
e.g
B = [17 4 1 3 2 3 2 1 1 1 1 1 1 1 1 1 1 2 .........12 1 15 372 3 1 1 1 2 42 93 1 105 5 1 1 1 1 78 1 2]

Réponse acceptée

Matt Fig
Matt Fig le 22 Juin 2011
What else does your data look like? Is it ones and zeros? Integers only? A single row vector, or matrix? Give all the details you can...
%
%
%
%
EDIT With more details added...
x = B==1; % Sequences of 1s, change this line to find other values.
t = [1 diff(x)]~=0;
x = x(t);
t = find([t 1]);
n = diff(t);
idx = (x==1 & n>=5); % Seq. length >= 5, change this for other lengths.
n = n(idx) % Length of each sequence of 1s.
idx = t(idx) % Starting index of each sequence.
  2 commentaires
bugguts99
bugguts99 le 22 Juin 2011
The data is a single row vector (1 X 14620); A range of integers where min value = 1 and max value = 6578.
bugguts99
bugguts99 le 22 Juin 2011
Thanks Matt. Works perfectly.

Connectez-vous pour commenter.

Plus de réponses (3)

Image Analyst
Image Analyst le 22 Juin 2011
If you have the Image Processing Toolbox, it's trivial. Just call bwlabel and regionprops (asking for Area and PixelIdxList), find those with area >= 5, and you're done in only about 3 lines of code, with the indexes of all 1's in a run of 5 or more.
  1 commentaire
bugguts99
bugguts99 le 22 Juin 2011
Also works perfectly when
x = B == 1;
Thanks for the tips!

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 22 Juin 2011
See the techniques described in this other recent Question
By the way:
endpos = find(B(S3(1):end)~=1,1);
if endpos is empty then everything to the end of B is 1's.

Andrei Bobrov
Andrei Bobrov le 22 Juin 2011
x = B == 1;
idx1int = [strfind([0 x],[0 1]); strfind(x,[1 0])];
idxout = idx1int(:,diff(idx1int,1)+1>=5);
nout = diff(idxout,1)+1;

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by