Select sequence of numbers from Array

Lets say I have an array like so:
[1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4]
(my real array is 3635 long)
I want to make a new array which shows which indexes have an nth time of sequence of 1's.
So like this:
the 3rd time the 1's are in sequence so in this case i would like it to return: [37 38 39]

댓글 수: 1

Dyuman Joshi
Dyuman Joshi 2023년 2월 2일
Is the real array same/similar as this one? Because there's a repeating pattern here so you can easily get the indices.

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

답변 (1개)

Stephen23
Stephen23 2023년 2월 2일

0 개 추천

V = [1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4];
X = V(:)==1;
Y = cumsum(diff([0;X])>0);
Z = 1:numel(V);
C = accumarray(Y(X),Z(X),[],@(a){a})
C = 4×1 cell array
{3×1 double} {3×1 double} {3×1 double} {3×1 double}
C{:}
ans = 3×1
1 2 3
ans = 3×1
13 14 15
ans = 3×1
25 26 27
ans = 3×1
37 38 39

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2023년 2월 2일

댓글:

2023년 2월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by