MATLAB Answers

How to write an automatic function based on the median of data to form groups of data.

1 view (last 30 days)
zhoug zho
zhoug zho on 21 Jun 2021
Commented: zhoug zho on 21 Jun 2021
How to write an automatic function based on the median of data to form groups of data.
as the boxplots are here in this figure, i need to make groups or clusters of data based on median. like once i have median of data, then i need to identify the beginning of a new cluster/group when the median delay increase instead of decresing. and when the median again increases then from that point a new group or cluster will start.
i manually grouped the data based on median, i need to make function like this.
Thanks in advance.

Accepted Answer

Adam Danz
Adam Danz on 21 Jun 2021
Edited: Adam Danz on 21 Jun 2021
% Create demo data : nxm matrix with 1 box per column
rng('default')
data = rand(100,15) + [1.0 1.3 0.8 0.95 1.02 1.05 1.08 1.2 1.0 1.03 1.06 0.90 0.95 0.80 1.1];
% Compute median of each column
dataMedians = median(data);
% Create boxplot
h = boxplot(data);
% Add group separation lines
groupStart = diff([inf,dataMedians])<0;
groupIdx = find(groupStart);
for i = 1:numel(groupIdx)
xline(groupIdx(i)-.5, 'k--', 'LineWidth',1)
end
% Color the groups
colors = lines(sum(groupStart));
groupID = cumsum(groupStart);
for i = 1:sum(groupStart)
set(h(:,groupID==i), 'Color', colors(i,:))
end
Using boxchart with group separation lines (for color control see demo1, demo2, documentation demo.
% Create demo data : nxm matrix with 1 box per column
rng('default')
data = rand(100,15) + [1.0 1.3 0.8 0.95 1.02 1.05 1.08 1.2 1.0 1.03 1.06 0.90 0.95 0.80 1.1];
% Compute median of each column
dataMedians = median(data);
% Create boxplot
figure()
h = boxchart(data);
% Add group separation lines
groupStart = diff([inf,dataMedians])<0;
groupIdx = find(groupStart);
for i = 1:numel(groupIdx)
xline(groupIdx(i)-.5, 'k--', 'LineWidth',1)
end

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by