Extracting data from part of a plot

76 次查看(过去 30 天)
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?

采纳的回答

Apurba Paul
Apurba Paul 2022-8-8
I have figured out a way. You can actually select data direclty from plot using Brush/Select data tools, whcich can be accessed through the top right corner of the current axis. You can then drag and select any part of the data, and then you can copy, delete or manipulate the data. See the figure bellow.
For more detail see the following link:

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-8-6
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 个评论
Apurba Paul
Apurba Paul 2022-8-8
@Walter Roberson, this seems a nice idea to use kmeans to find the cluster. But for this perticular workflow, it will be much easier for me if I can direcltly select the data from plot. Thankfully I have figured out a way. You can actually select data direclty from plot using Brush/Select data tools, whcich can be accessed through the top right corner of the current axis. You can then drag and select any part of the data, and then you can copy, delete or manipulate the data. See the figure bellow.
For more detail see the following link:

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by