MATLAB: Splitting a vector at sign change

MATLABvector separation

Hi!
I have a vector with values > 0 and values < 0 such as for example:
K = [1 2 3 4 5 6 7 8 -1 -2 -3 -4 -5 -6 -7 -8 1 2 3 4 5 6 7 8] and so on….
I want to split this vector to n new vectors where the sign changes from + to – and vice versa such that:
N1 = [1 2 3 4 5 6 7 8]
N2 = [-1 -2 -3 -4 -5 -6 -7 -8]
N3 = [1 2 3 4 5 6 7 8]
The K vector does not have a set number of positive or negative values between each sign change.
Any suggestions?
Regards

Best Answer

K = [1 2 3 4 5 6 7 8 -1 -2 -3 -4 -5 -6 -7 -8 1 2 3 4 5 6 7 8] ;
K = K' ;
S = sign(K) ;
C = mat2cell(S, diff([0; find(diff(S)); size(S,1)]));
L = cellfun(@numel,C) ;
iwant = mat2cell(K,L) ;
celldisp(iwant)