Select sequence of numbers from Array
4 views (last 30 days)
Show older comments
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 Comment
Dyuman Joshi
on 2 Feb 2023
Is the real array same/similar as this one? Because there's a repeating pattern here so you can easily get the indices.
Answers (1)
Stephen23
on 2 Feb 2023
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{:}
0 Comments
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!