Skip to content

Instantly share code, notes, and snippets.

@jpeoples
Created September 24, 2019 23:26
Show Gist options
  • Save jpeoples/c25f9cba36519b2c223349904961df57 to your computer and use it in GitHub Desktop.
Save jpeoples/c25f9cba36519b2c223349904961df57 to your computer and use it in GitHub Desktop.
Compute the statistics used in matlab boxplots.
function [q1,q2,q3,w0,w1,outliers] = boxplot_statistics(data, whisker)
if ~exist('whisker', 'var')
% whisker is 1.5 by default
whisker = 1.5;
end
% quantile(data,3) will return the 25th, 50th, and 75th percentile
% for each column
quants = quantile(data, 3);
q1 = quants(1,:);
q2 = quants(2,:);
q3 = quants(3,:);
% Compute the upper and lower thresholds for outlier classification
upper_thresh = q3 + whisker .* (q3-q1);
lower_thresh = q1 - whisker .* (q3-q1);
% Outliers are points above the upper_thresh or below lower_thresh
outliers = (data > upper_thresh) | (data < lower_thresh);
% To compute the whiskers, take max and min (per column). Setting
% outlier values to NaN causes them to be ignored.
data(outliers) = NaN;
w0 = min(data,[],1);
w1 = max(data,[],1);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment