I want to get how many times a value sequence is repeated in a column and the average number of times it repeated in a sequence.
3 views (last 30 days)
Show older comments
suppose i have a column
[0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 ]
i want to get , that the repetative sequence of 1 is repeated 5 times in the column. (So here answer should be 5)
and the average number of times it repeated in a sequence is (4+3+3+4+3)/5 = 3.4 (So here answer should be 3.4)
Sorry for my english!
0 Comments
Answers (3)
Walter Roberson
on 4 Mar 2022
C = [0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 ]
%for this purpose, C MUST be a row vector
starts = strfind([0 C], [0 1]);
stops = strfind([C 0], [1 0]);
number_of_repeats = length(starts)
average_repeats = mean(stops - starts + 1)
%alternative, needs Image Processing Toolbox
%for this purpose C could be a row or column vector
info = regionprops(logical(C), 'Area');
number_of_repeats = length(info)
average_repeats = mean([info.Area])
0 Comments
Voss
on 4 Mar 2022
You can use strfind() to find a pattern in a vector like this. Use pattern [0 1] to find where the 1's start, and use pattern [1 0] to find where the 1's end. Prepend and append a 0 to x to correctly handle the cases when x starts/ends with a 1.
x = [0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0].';
sequence_start = strfind([0 x(:).' 0],[0 1])
sequence_end = strfind([0 x(:).' 0],[1 0])
sequence_length = sequence_end-sequence_start
n_sequences = numel(sequence_length)
avg_sequence_length = mean(sequence_length)
0 Comments
Stephen23
on 4 Mar 2022
"suppose i have a column"
V = [0;0;0;0;0;0;0;0;0;1;1;1;1;0;0;0;0;0;0;0;1;1;1;0;0;0;0;0;0;0;1;1;1;0;0;0;0;1;1;1;1;0;0;0;0;0;0;1;1;1;0;0;0];
D = find(diff([0;V(:);0]));
M = mean(D(2:2:end)-D(1:2:end))
N = numel(D)/2
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!