필터 지우기
필터 지우기

How to count the number of consecutive repetitions of an array?

조회 수: 4 (최근 30일)
giovanni negro
giovanni negro 2018년 4월 12일
댓글: Walter Roberson 2021년 1월 15일
Hi everybody,
I have a double array or a logical one made like this above: A=[0 1 1 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1]; And I would like to count the number of consecutive repetitions of values 1, getting as output an array like this: B=[3 1 2 1 3], where 3=3 times 1; 1=1 time 1; ecc.. Please, can someone help me with this issue?
Thanks a lot!

채택된 답변

James Tursa
James Tursa 2018년 4월 12일
E.g.,
[r,s] = runlength(A,numel(A));
result = r(logical(s));
You can find runlength on the FEX:
  댓글 수: 7
Walter Roberson
Walter Roberson 2021년 1월 15일
Your A vector contains values between 1 and 235, not the 0 and 1 of the original Question. What is it that you want to compute on your A?
Walter Roberson
Walter Roberson 2021년 1월 15일
Your A vector has no runs at all. Every value that occurs, occurs in isolation, with there never being two of the same value in a row. You cannot do any useful run-length encoding on it -- not unless you are able to go into pairs of values instead of single values.
load a.mat
results = {};
for targ = unique(A).'
a = A.' == targ;
starts = strfind([0 a], [0 1]);
if isempty(starts); continue; end
stops = strfind([a 0], [1 0]);
results{end+1, 1} = [targ, stops - starts + 1];
end
celldisp(results)

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by