Identifying time for prolong changes in a sequence

Hello
Please I have a sequence for instance
010001100000111110000100011001
And I want to identify the ones that are more than 2(onces) and the time
for this example is 5 seconds
Also another example is when I have more than one occurances of 1 greater than 2
000001110001111001000110001
Then the time are [ 3sec 4sec]
I have used the code A = seconds(x)
It only shows me a total number of 1 in seconds.
Is there a way I can get about this.
Thanks for your help in advance

 Accepted Answer

Try this (requires the Image Processing Toolbox):
binarySequence = [0 1 0 0 0 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 0 0 0 1 1 0 0 1];
props = regionprops(logical(binarySequence), 'Area');
allLengths = [props.Area]
% Count number of regions longer than some number:
maxLength = max(allLengths)
for k = 1 : maxLength
longerThanN = sum(allLengths == k);
fprintf('There are %d sequences of %d.\n', longerThanN, k);
end
You get:
There are 3 sequences of 1.
There are 2 sequences of 2.
There are 0 sequences of 3.
There are 0 sequences of 4.
There are 1 sequences of 5.

10 Comments

Tino
Tino on 8 Jul 2021
Edited: Tino on 8 Jul 2021
Thanks a lot for your time and effort.
What if I want an sequence of the output. For instance if I want to list the (There are 1 sequences of 5) to become
000000000000111110000000000000
or
There are 3 sequences of 1 to become
010000000000000000000100000001
thanks for your time in advance
You can use bwlabel and ismember. Write back if you can't figure it out.
Hi
I cannot figure out the bwlabel and ismember.
Can you assist me on this if possible?
Thanks
Actually you can just ask regionprops() for the locations:
binarySequence = [0 1 0 0 0 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 0 0 0 1 1 0 0 1];
% Label the data
props = regionprops(logical(binarySequence), 'Area', 'PixelIdxList');
allLengths = [props.Area]
% Count number of regions longer than some number:
maxLength = max(allLengths)
for k = 1 : maxLength
% Get the labels for this length
labels = allLengths == k;
longerThanN = sum(labels);
fprintf('There are %d sequences of %d.\n', longerThanN, k);
% Get 1's wherever the run has a length of k.
locations = zeros(size(binarySequence));
for k2 = 1 : length(labels)
if labels(k2)
locations(props(k2).PixelIdxList) = 1;
end
end
% Report to the command window:
locations
end
If you want locations in a 2-D array, you can do this:
binarySequence = [0 1 0 0 0 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 0 0 0 1 1 0 0 1];
% Label the data
props = regionprops(logical(binarySequence), 'Area', 'PixelIdxList');
allLengths = [props.Area]
% Count number of regions longer than some number:
maxLength = max(allLengths)
locations = zeros(maxLength, length(binarySequence));
for k = 1 : maxLength
% Get the labels for this length
labels = allLengths == k;
longerThanN = sum(labels);
fprintf('There are %d sequences of %d.\n', longerThanN, k);
% Get 1's wherever the run has a length of k.
for k2 = 1 : length(labels)
if labels(k2)
locations(k, props(k2).PixelIdxList) = 1;
end
end
end
% Report to the command window:
locations
and you get
locations =
Columns 1 through 23
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0
Columns 24 through 30
0 0 0 0 0 0 1
0 0 1 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Note the length is the row number. So row 2 has 1's wherever those locations are in a run of 2 digits long, namely 1's in columns 6 & 7, and 26 & 27.
Tino
Tino on 11 Jul 2021
Edited: Tino on 11 Jul 2021
Thank you very much sir.
Very grateful.
Another question if it is possible? what of identifying numbers that have conditions
for instance if 1000011111000001111000000110000000111100
if 1 > 3
The answer becomes
0000001111100000111100000000000000111100
Thank you for your help so far
Another question if it is possible? what of identifying numbers that have conditions
for instance if 1000011111000001111000000110000000111100
if 1 > 3
The answer becomes
0000001111100000111100000000000000111100
Thank you for your help so far
binarySequence2 = bwareaopen(binarySequence, 4); % Keep regions >3, meaning 4 or longer.
Thank you very much
Hi Image analyst.
If I wanted only onces to show (and there group occurences in the sequence) in the result instead of both 1 and zero
for instance if I have 1111100001111000000111111
I will like the results to be
There are 1 sequences of 4 = second occurences
There are 2 sequences of 5 = first and third occurences
I will appreciate it if you answer the question for me as you ve done in the past
Best wishes
Try this:
binarySequence = [0 1 0 0 0 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 0 0 0 1 1 0 0 1]
% Measure the data
props = regionprops(logical(binarySequence), 'Area', 'PixelIdxList');
allLengths = [props.Area]
% Count number of regions longer than some number:
maxLength = max(allLengths)
for k = 1 : maxLength
% Get the labels for this length
labels = allLengths == k;
longerThanN = sum(labels);
fprintf('There are %d sequences of %d.\n', longerThanN, k);
indexes{k} = find(labels);
for k2 = 1 : length(indexes{k})
fprintf(' Sequence %d\n', indexes{k}(k2));
end
% Get 1's wherever the run has a length of k.
locations = zeros(size(binarySequence));
for k2 = 1 : length(labels)
if labels(k2)
locations(props(k2).PixelIdxList) = 1;
end
end
% Report to the command window:
%locations
end
There are 3 sequences of 1.
Sequence 1
Sequence 4
Sequence 6
There are 2 sequences of 2.
Sequence 2
Sequence 5
There are 0 sequences of 3.
There are 0 sequences of 4.
There are 1 sequences of 5.
Sequence 3

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!