Selecting the nearest data point in a 2-D plot

4 ビュー (過去 30 日間)
Sanjoy Jena
Sanjoy Jena 2019 年 4 月 11 日
編集済み: Ajay Kumar 2019 年 11 月 5 日
Hi,
Is it possible to select the nearest data point rather select any point on the graph? I have seen "ginput" and "getpts" function. Both of these select the point that we click not the nearest data point.
For example you can view thw bottom code
x = 0:0.1:10;
for C=1:3
za = C;
y = (x.^za);
hg = figure(1);
plot(x,y);hold on;
[xda(:,C),yda(:,C)] = ginput(2);
%[xda(:,C),yda(:,C)] = getpts(hg);
display(za);
end

回答 (1 件)

Ajay Kumar
Ajay Kumar 2019 年 11 月 5 日
編集済み: Ajay Kumar 2019 年 11 月 5 日
Try to perform difference (and take minimum of absolute) between both ginput data point and original data and take out the index.
[val,idx]=min(abs(ginput_data - original_data));
then use this idx to go to the original data (which is the nearest point) instead of any point on axes.
  2 件のコメント
darova
darova 2019 年 11 月 5 日
Using difference you just find min(dx) and min(dy). Use minimum distance instead (e.g. pdist2)
Ajay Kumar
Ajay Kumar 2019 年 11 月 5 日
編集済み: Ajay Kumar 2019 年 11 月 5 日
Also, a small addition to darova's comment. pdist2 needs Statistcs and machine learning toolbox.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by