I need to find a pattern in a column

조회 수: 1 (최근 30일)
quentin bruxelles
quentin bruxelles 2021년 11월 8일
댓글: Chris 2021년 11월 9일
Hello,
I need to count the number of repetition of a pattern in my data. The pattern is a serie of 35 zeros. The problem is that I didn`t find how to do it in column cause my data are un one column.
Thank you for your help

채택된 답변

Chris
Chris 2021년 11월 8일
A = readmatrix('test.xlsx');
count = 0;
for idx = 1:numel(A)-34
if sum(A(idx:idx+34)) == 0
count = count+1;
end
end
If there is a series of N>35 zeros, this wll count the pattern multiple times (for example, if there are 36 zeros in a row, that's two patterns of 35).
Also, there are probably more efficient ways to do this.
  댓글 수: 2
quentin bruxelles
quentin bruxelles 2021년 11월 9일
it is possible to not count the same thing multiple times ?
Chris
Chris 2021년 11월 9일
certainly!
Maybe something like this:
A = readmatrix('test.xlsx');
count = 0;
for idx = 1:numel(A)-34
% The previous condition, but also either:
% (we're at the first index OR the previous index isn't also 0)
if (sum(A(idx:idx+34)) == 0 && (idx==1 || A(idx-1) ~= 0))
count = count+1;
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by