Extracting data from part of a plot
Show older comments
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?
Accepted Answer
More Answers (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])
1 Comment
Apurba Paul
on 8 Aug 2022
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


