Clear Filters
Clear Filters

Splitting a vector into unequal sections with values greater than or equal to 2 for at-least next 5 cells

1 view (last 30 days)
I was looking for help to split a vector into sections separated by values greater than or equal to 2 for next 5 cells. If A = [ 0 0 0 1 2 0 1 2 3 4 5 6 7 8 6 4 2 1 0 0 0 0 1 2 2 2 3 5 5 5 4 3 2 0 0 0], then my sections should be 1 = [2 3 4 5 6 7], 2= [2 2 2 3 5 5].
and how to call these sections for further operations like math operation.

Accepted Answer

Jos (10584)
Jos (10584) on 22 Oct 2016
First create a vector that will be true when an element x of A and the next N elements are larger than a values V
N = 5 ; V = 2 ;
TF = arrayfun(@(x) all(A(x:x+N-1)>=V),1:numel(A)-N+1)
Secondly, we will find the start and endpoints of each sequence of ones in TF
SP = find([1 TF]==0 & [TF NaN]==1)
EP = find([NaN TF]==1 & [TF 0]==0)-1
Finally, we use those indices to retrieve the sections of A and put them in a cell array, as these sections may not be of the same size
B = arrayfun(@(a,b) A(a:b),SP,EP,'un',0)
B{1} % section 1

More Answers (0)

Categories

Find more on Tables 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!