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

조회 수: 1(최근 30일)
zhoug zho 2021년 6월 21일
댓글: zhoug zho 2021년 6월 21일
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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

편집: Adam Danz 2021년 6월 21일
% 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);
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);
groupStart = diff([inf,dataMedians])<0;
groupIdx = find(groupStart);
for i = 1:numel(groupIdx)
xline(groupIdx(i)-.5, 'k--', 'LineWidth',1)
end
##### 댓글 수: 1표시숨기기 없음
zhoug zho 2021년 6월 21일
Thank you so much Adam Danz .

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by