Grouping Matlab Code and Algorithm