# Create a new array everytime with the change of sign in an array

1 view (last 30 days)
Sri Vishnu Katreddi on 29 Apr 2022
Edited: Matt J on 29 Apr 2022
I have an array A = [0 1 2 3 2 1 0 -1 -2 -2 -1 0 1 1.6 1.8 2 1 0 .... ], Now I want to create a new array everytime the sign changes in the array, it should look like
B = [0 1 2 3 2 1 0]
C = [-1 -2 -2 -1]
D = [0 1 1.6 1.8 2 1 0]
.
.
.
##### 2 CommentsShow 1 older commentHide 1 older comment
Sri Vishnu Katreddi on 29 Apr 2022
Edited: Sri Vishnu Katreddi on 29 Apr 2022
0 can either take run of positives or run of negatives, In fact I am trying to create based on the negative sign.

Matt J on 29 Apr 2022
Edited: Matt J on 29 Apr 2022
and then do like in the following:
A = [0 1 2 3 2 1 0 -1 -2 -2 -1 0 1 1.6 1.8 2 1 0];
G=groupConsec(A>=0);
output=groupFcn(@(x) {x}, A ,G); %return the groups as cell array elements,
%not separate variables
>> output{:}
ans =
0 1 2 3 2 1 0
ans =
-1 -2 -2 -1
ans =
0 1.0000 1.6000 1.8000 2.0000 1.0000 0
Sri Vishnu Katreddi on 29 Apr 2022
Thank you very much.

### Categories

Find more on Lighting, Transparency, and Shading in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!