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 ビュー (過去 30 日間)
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!

採用された回答

Walter Roberson
Walter Roberson 2022 年 3 月 4 日
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 ]
C = 1×53
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
%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)
number_of_repeats = 5
average_repeats = mean(stops - starts + 1)
average_repeats = 3.4000
%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)
number_of_repeats = 5
average_repeats = mean([info.Area])
average_repeats = 3.4000

その他の回答 (2 件)

Voss
Voss 2022 年 3 月 4 日
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_start = 1×5
10 21 31 38 48
sequence_end = strfind([0 x(:).' 0],[1 0])
sequence_end = 1×5
14 24 34 42 51
sequence_length = sequence_end-sequence_start
sequence_length = 1×5
4 3 3 4 3
n_sequences = numel(sequence_length)
n_sequences = 5
avg_sequence_length = mean(sequence_length)
avg_sequence_length = 3.4000

Stephen23
Stephen23 2022 年 3 月 4 日
"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))
M = 3.4000
N = numel(D)/2
N = 5

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by