如何在Matlab中从大型散点图中找到索引?

4

我有一个散点图,大约有6000个数据点。

x = rand(1,6000);
y = rand(1,6000);
scatter(x,y)

是否有一种方法可以使用GUI找到给定点的索引?(我们将数据放大,想要找到产生该点的特定索引)

2个回答

6

这里有一个非常简单的解决方案:

打开绘图 > 数据光标 > 编辑文本更新函数

将文本更新函数设置为:

function output_txt = myFunction(obj,event_obj)
% Display the position of the data cursor
% obj          Currently not used (empty)
% event_obj    Handle to event object
% output_txt   Data cursor text string (string or cell array of strings).

pos = get(event_obj,'Position');

% Import x and y
x = get(get(event_obj,'Target'),'XData');
y = get(get(event_obj,'Target'),'YData');

% Find index
index_x = find(x == pos(1));
index_y = find(y == pos(2));
index = intersect(index_x,index_y);

% Set output text
output_txt = {['X: ',num2str(pos(1),4)], ...
              ['Y: ',num2str(pos(2),4)], ...
              ['Index: ', num2str(index)]};

% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
    output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end

结果:

这里输入图片描述

顺便说一下,如果您想以编程方式完成此操作,那么这里有一篇很好的文章here


如果它在该x值处随机化了多个坐标怎么办?虽然不太可能,但这种情况确实可能发生。 - Fantastic Mr Fox
@Ben 我考虑了一下,你也可以使用 y 坐标的数据...我会更新它。这种情况发生的概率 "几乎肯定" 是零。 - JustinBlaber
1
注意:如果您使用{x = get(get(event_obj,'Target'),'XData');}而不是上面的那行代码,您可以使其适用于任何散点图,而不仅仅是在上述使用x/y基本命名空间约定的散点图上。 - John
@John 很好的笔记,我通常不使用 evalin,但在这种情况下,它是一个非常简单的解决方案。在我看来,获取 XData 属性肯定更好。 - JustinBlaber
@jucestain 完全理解。这是 Stack Overflow 的乐趣之一,我们可以改进答案!尽管我对您的答案提出的建议性修改,包括代码更改被删除了... :( - John

0

您可以使用X、Y位置来搜索该点:

%export the cursor to the workspace

possibleXpositions =  find(x == cursor_info.Position(1));
possibleYPositions = find(y == cursor_info.Position(2));
position = intersect(possibleXpositions, possibleYPositions);

position将保存您选择的随机数的索引。

作为一行代码:

position = intersect(find(x == cursor_info.Position(1)), find(y == cursor_info.Position(2)));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接