Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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.

Respuesta aceptada

Jos (10584)
Jos (10584) el 22 de Oct. de 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

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by