Splitting a vector into unequal sections with values greater than or equal to 2 for at-least next 5 cells
2 次查看(过去 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.
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!