# MATLAB: Identifying change of sign of a data and make a group for every 2 changes of sign.

change of sign

Hi, Well as my question says, i need help to make groups of data for every 2 changes of sign, for example i have this data:
 0.1  -0.16966 0.2  -0.072523 0.3  0.21066 0.4  -0.06312 0.5  -0.14626 0.6  0.14327 0.7  0.010853
And i want a code that make me this following groups:
 Group 1: -0.16966, -0.072523, 0.21066 Group 2: -0.06312, -0.14626, 0.14327, 0.010853
And then, find the max and min for each group, add them, find the average of all group:
 G=matrix with all the groups a=max(G) b=min(G) A=mean(a+abs(b))
I have done a code which makes this but in order to work i need to put Zeros value in between of a change of sign and i have like 300 points so it takes time,
the code that i have right now is:
 clear all close all %--------------------------OPTIMIZATION OF TMD----------------------------% %Load files% for u = 0:20:220;    C= strcat('TMD_',num2str(u),'.csv');    load(C); end %------C=0 N*s/m------% c0=0; t0=TMD_0(:,2); %seconds% vy0=TMD_0(:,3); %m/s% allzeros = find(vy0 == 0); x0 = allzeros(1:2:end); z0 = zeros(length(x0),1); a0 = zeros(length(x0),1); for i=1:length(x0)-1 y0 = vy0(x0(i):1:x0(i+1)); z0(i) = max(y0);a0(i) = min(y0);     end yend0 = vy0(x0(end-1):1:x0(end)); z0(length(x0)) = max(yend0); a(length(x0)) = min(yend0); B0 = z0+abs(a0); A0 = mean(B0);
Thanks for the ideas!

• %generate some data for demo purpose:x = 0:3*360;data = x.*(sind(x)+cosd(2*x));plot(data);datasign = sign(data); %get the sign of the data. %find where it goes from -1 to 1 or 1 to -1. Problem is there may be some 0 in between%so first remove the zero, but remember where everything was:[~, oldindices, nozerosign] = find(datasign);%now find the transitions in nozerosign. It's where the diff is non-nullzerocross = find(diff(nozerosign));%transform the indices in nozerosign into indices in datazerocross = oldindices(zerocross)+1;%we now know where every zero crossing occurs, split the data accordinglysplitboundaries = [1 zerocross(2:2:end) numel(data)];splitcount = numel(splitboundaries)-1;localmins = zeros(1, splitcount);localmaxs = zeros(1, splitcount);for splitidx = 1:splitcount  splitdata = data(splitboundaries(splitidx):splitboundaries(splitidx+1));  localmins(splitidx) = min(splitdata);  localmaxs(splitidx) = max(splitdata);end