Extracting data from part of a plot
이전 댓글 표시
I want to extract data point from a part of a plot. If I plot my data I can see the data points are distributed in groups or cluster. I want to extract one such cluster for further processing.
For example, lets look at the following code.
x=[1,10,20,3,11,23,4,12,13,13,24]+rand(1,11);
y=[2,7,21,1,8,24,3,9,8,10,22]+rand(1,11);
plot(x,y,'ro')
Now suppose I want to extract the data points of the middle group for further processing. Is it possible in matlab, if yes how can I do it?
채택된 답변
추가 답변 (1개)
Do they need to be extracted from the plot or can we use x and y instead?
Is the number of groups known? This code assumes that it is.
x=[1,10,20,3,11,23,4,12,13,13,24]+rand(1,11);
y=[2,7,21,1,8,24,3,9,8,10,22]+rand(1,11);
num_groups = 3;
%cluster them to figure out which ones belong together
[IDX, C] = kmeans([x(:), y(:)], num_groups);
%but which one is the "center" group? Find the center that
%is closest to the centroid of the data ?
D = pdist2(C, [mean(x(:)), mean(y(:))]);
[~, centeridx] = min(D);
%extract items that are in that center group
xc = x(IDX == centeridx);
yc = y(IDX == centeridx);
scatter(xc(:), yc(:), 'b+')
xlim([min(x)-1, max(x)+1])
ylim([min(y)-1, max(y)+1])
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



