Statistics Toolbox    

Function for Grouped Data

As we saw in the previous section, the descriptive statistics functions can compute statistics on each column in a matrix. Sometimes, however, you may have your data arranged differently so that measurements appear in one column or variable, and a grouping code appears in a second column or variable. Although the MATLAB syntax makes it simple to apply functions to a subset of an array, in this case it is simpler to use the grpstats function.

The grpstats function can compute the mean, standard error of the mean, and count (number of observations) for each group defined by one or more grouping variables. If you supply a significance level, it also creates a graph of the group means with confidence intervals.

As an example, load the larger car data set. We can look at the average value of MPG (miles per gallon) for cars grouped by org (location of the origin of the car).

We can also get the complete set of statistics for MPG grouped by three variables: org, cyl4 (the engine has four cylinders or not), and when (when the car was made).


  Functions for Data with Missing Values (NaNs) Percentiles and Graphical Descriptions