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 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
shriramlkarni kulkarni
am 4 Mär. 2022
Beantwortet: Stephen23
am 4 Mär. 2022
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 4 Mär. 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 Kommentare
Weitere Antworten (2)
Voss
am 4 Mär. 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 Kommentare
Stephen23
am 4 Mär. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!