How to find the number of repeating sequence in an array?
8 次查看(过去 30 天)
显示 更早的评论
A sequence is
011011011001111011111001001011001011011001101001011011001111111.
I'm looking for a sequence of 111 from the above. But the scanning can be done by considering the first three elements(011) and then next three elements(011) and so on.
That is
011,011,011,001,111,011,111,001,001,011,001,011,011,001,101,001,011,011,001,111,111.
Thus four occurrences. Similarly for 1111. Appreciate help.
0 个评论
采纳的回答
kowshik Thopalli
2017-3-5
编辑:kowshik Thopalli
2017-3-5
You can do the following. Treat the sequence as a string. Divide the string into a cell with each cell containing 3 digits. Then use strfind. But this assumes the length of sequence you wish to find is a factor for the total length. Your last line says 1111 but the length of sequence is 63. 63 is not divisible by 4. I Assume you like to do this only till 1:60 of your sequence.
Sequence='011011011001111011111001001011001011011001101001011011001111111';
required='1111'; len_required= length(required);
len_sequence=length(Sequence);
end_point=len_sequence- mod(len_sequence,len_required);
Cell_sequence=cellstr(reshape(Sequence(1:end_point),len_required,[])'); % this will divide the sequence with each cell having 3 digits
Find_sequence=cellfun(@(x) strfind(x,required), Cell_sequence,'UniformOutput',false);
output= sum(cell2mat(Find_sequence))
Hope this helps!
3 个评论
Image Analyst
2017-3-5
That's the demo he gave. Unless you wanted to detect the pattern in an overlapping way, rather than in "jumps" of 4. So like 11111 would be 2 instances of 1111. Please explain.
更多回答(1 个)
Image Analyst
2017-3-5
You could use histogram():
% Define data.
s = '011011011001111011111001001011001011011001101001011011001111111';
% Figure out how many times each 3 character string shows up.
s3 = reshape(s, 3, [])'
n3 = arrayfun(@str2double, s3)
numbers = n3(:,1)*100 + n3(:, 2)*10 + n3(:, 3) % Convert to numbers.
h = histogram(numbers, 'BinEdges', 0:112) % Count how many times each number occurs.
grid on;
% DONE!
% Just to check, let's print it out to the command window.
for bin = 1 : length(h.Values)
if h.Values(bin) > 0
fprintf('%3.3d shows up %d times.\n', h.BinEdges(bin), h.Values(bin));
end
end
It prints out:
001 shows up 7 times.
011 shows up 9 times.
101 shows up 1 times.
111 shows up 4 times.
4 个评论
Image Analyst
2017-3-5
kowshik, I thought that was a typo, because like you said, the sequence doesn't make sense if you take things in groups of 3 when you're searching for something 4 characters long. The sequence is not a multiple of 4. He didn't say to take them in groups of 4, or 5, or whatever. He said groups of 3. But whatever...
kowshik Thopalli
2017-3-5
Image Analyst, Thanks for all of your answers. I have immensely benefited through them over years. I know this isnt the right place but can you answer one of my questions regarding the Image datastores. I wish to join two image datastores.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!