【问题标题】:How to find the index from a large scatter plot in matlab?如何从matlab中的大散点图中找到索引?
【发布时间】:2013-03-07 11:55:39
【问题描述】:

我有一个包含大约 6000 个项目的散点图。

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

有没有办法使用 GUI 找到给定点的索引? (我们放大数据,并希望找到产生某个点的特定索引)

【问题讨论】:

    标签: matlab scatter-plot


    【解决方案1】:

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

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

    设置文字更新功能为:

    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 值处随机化多个坐标怎么办。不太可能,但它可能会发生。
    • @Ben 我想过这个,你也可以使用 y 坐标的数据......我会更新它。这种情况发生的概率“几乎肯定”为零。
    • 注意:如果您使用 {x = get(get(event_obj,'Target'),'XData');} 而不是上面的行,则可以在任何散点图上进行此操作,而不是只是在上面使用 x/y 基本命名空间约定的一种。
    • @John 很好,我通常不使用 evalin,但在这种情况下,它是一个非常简单的解决方案。通过 imo 获取 XData 属性肯定更好。
    • @jucestain 完全明白这一点。这就是堆栈溢出的乐趣之一,我们可以改进答案!虽然,我为您的答案提议的编辑,其中包括代码更改被删除了...... :(
    【解决方案2】:

    可以使用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 = intersect(find(x == cursor_info.Position(1)), find(y == cursor_info.Position(2)));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-12
      • 2013-12-17
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多