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

조회 수: 1 (최근 30일)
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.

채택된 답변

Jos (10584)
Jos (10584) 2016년 10월 22일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by