function c = grpSplit(data, grp)
% Split rows of matrix by grouping variable into cell array
%
% c = grpSplit(data, grp)
%
% data is a matrix with the same number of rows as elements of grp. grp
% contains group membership for each row of data. c{i} contains the subset
% of data whose rows have the same group membership. The length of c is
% equal to the number of unique groups in grp. the order of groups is the
% sorted order of unique groups in grp

% Copyright 2014 MathWorks, Inc.

uGrp = unique(grp);
nGrp = length(uGrp);
c = cell(1, nGrp);
for i = 1:nGrp
    c{i} = data(grp == uGrp(i),:);
end