フィルターのクリア

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 Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by