# Subdivide group of data

6 views (last 30 days)
I am having trouble subdiving into sub-groups my data. The first thing I was trying to do is selecting group of data every 30 degrees from 0 to 360. It makes 12 groups of data. The secong thing I would like to do is from this group of data do another selection. For each 30 degrees I would like to divide into 5 groups. Thus have 12 Groups of data and from each group have 5 sub groups. I would like to do, for every 30 degrees residual groups (azimuth), the median of the residuals every 50m (epicenter distance). For example for the group of data with residuals that are between 0-30 degrees I would like to do the median of these residuals with epicentral distance from 0-50, 50-100,..., 250-300. This is the code I tried but it says that the indexe exceeds the matrix, I tried other things but have the same problem:
%dividing for epicenter distance from 0 to 300m every 50m
N_pieces3=6;
cheese_angle3=300/N_pieces3;
%diving for azimuth from 0 to 360
N_pieces=12;
cheese_angle=360/N_pieces;
%select groups of residuals every 30 degrees and epicentral distance every 30 degrees
for nps=1:N_pieces
resp1=resp(azmp1>=cheese_angle*(nps-1) & azmp1<=cheese_angle*nps);
depip1=depip(azmp1>=cheese_angle*(nps-1) & azmp1<=cheese_angle*nps);
%divind into 6 groups from the previous residual groups depending on the epicentral distance and compute its mean every 50m
for c=1:N_pieces3
resp3=resp1(depip>=cheese_angle3*(c-1) & depip<=cheese_angle3*c);
depip3=depip1(depip>=cheese_angle3*(c-1) & depip<=cheese_angle3*c );
angle3(c)=cheese_angle3*((c-1)+.5); %for plotting: the point must be at the center
Med(nps)=median(resp3)-medAs;
end
angle2=cheese_angle3*((nps-1)+.5); %for plotting: the point must be in the middle
end
At the end I would like to do a 3D plotting with the residuals median depending on the azimuth and the epicentral distance. If possible a 3D polar scatter. Thank you.

Razvan Carbunescu on 8 May 2018
If using R2018a you can compute the data easier by using groupsummary.
T = table(cheese,depip,resp);
GT = groupsummary(T,{'cheese','depip'},{12,5},'median','resp');
The above code should group cheese into 12 groups, within those group depip into 5 groups and then compute the median for the data stored in resp for each of the 12*5 groups.
For earlier releases can probably achieve a similar result by using some combination of findgroups / splitapply and discretize.
Razvan Carbunescu on 9 May 2018
If you want to do computation afterwards I'd say it's probably easier to convert both to double and use indexing to expand the new names to what you want
newcatscheese = (15:30:345)'; % making into column
GT.disc_cheese = newcatscheese(double(GT.disc_cheese))
If you want to keep them as categorical can probably change the names of the categories with renamecats.
newcatsdepip = {'25','75','125','175','225','275'};
GT.disc_depip = renamecats(GT.disc_depip,categories(GT.disc_depip),newcatsdepip)