如何在向量的所有点之间绘制线? [英] How do I plot lines between all points in a vector?

查看:131
本文介绍了如何在向量的所有点之间绘制线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量,其中包含二维空间中的某些点.我希望MATLAB使用从每个点到每个其他点绘制的线来绘制这些点.基本上,我想要一个连接所有顶点的图.您可以用积图来做吗,如果可以,怎么办?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. Can you do that with plot and if so, how?

推荐答案

一种解决方案是使用功能 LINE 来绘制每条线(它在给出的每一列数据上绘制一行):

One solution is to create a set of indices for every combination of points using the function MESHGRID. You can then plot each line using the function LINE (which plots one line per column of data it's given):

N = 10;                               %# Number of points
x = rand(1,N);                        %# A set of random x values
y = rand(1,N);                        %# A set of random y values
[I,J] = meshgrid(1:N);                %# Create all the combinations of indices
index = [I(:) J(:)].';               %'# Reshape the indices
line(x(index),y(index),'Color','k');  %# Plot the lines
hold on
plot(x,y,'r*');                       %# Plot the points

您可能会注意到,上述解决方案将为每个连接绘制一条线,这意味着它将绘制长度为零的连接点到自身的线,并且将为每个连接绘制2条线(即,从点开始) A指向B点从B点到A点).这是另一种解决方案(使用 HANKEL 查找),该图不会显示出多余或不必要的内容行:

You may notice that the above solution will plot a line for every connection, meaning that it will plot lines of zero length connecting points to themselves and will plot 2 lines for every connection (i.e. from point A to point B and from point B to point A). Here's another solution (using the functions HANKEL and FIND) that won't plot the redundant or unnecessary lines:

N = 10;                               %# Number of points
x = rand(1,N);                        %# A set of random x values
y = rand(1,N);                        %# A set of random y values
[r,c,v] = find(hankel(2:N));          %# Create unique combinations of indices
index = [v c].';                     %'# Reshape the indices
line(x(index),y(index),'Color','k');  %# Plot the lines
hold on
plot(x,y,'r*');                       %# Plot the points

以上两种解决方案均会创建视觉上相同的图:

Both of the above solutions create visually identical plots:

有关时间安排的提示...

出于好奇,我以为我应该 HANKEL 解决方案,并将其与 NCHOOSEK 解决方案.对于N = 10,没有明显的差异.但是,当我将N增大到更大的值时,我开始看到NCHOOSEK解决方案开始变得非常缓慢:

Out of curiosity, I thought I'd time my HANKEL solution and compare it with Amro's very terse NCHOOSEK solution. For N = 10, there was no appreciable difference. However, as I increased N to much larger values I began to see the NCHOOSEK solution start to become very slow:

  • N = 200

>> tic; [r,c,v] = find(hankel(2:N)); index = [v c].'; toc; %'
Elapsed time is 0.009747 seconds.

>> tic; pairs = nchoosek(1:N,2).'; toc; %'
Elapsed time is 0.063982 seconds.

  • N = 1000

    >> tic; [r,c,v] = find(hankel(2:N)); index = [v c].'; toc; %'
    Elapsed time is 0.175601 seconds.
    
    >> tic; pairs = nchoosek(1:N,2).'; toc; %'
    Elapsed time is 12.523955 seconds.
    

  • 我有点惊讶,直到我查看了NCHOOSEK的代码(通过在MATLAB命令窗口中键入type nchoosek).变量不仅会在循环内增长,而且不会被

    I was kind of surprised, until I looked at the code for NCHOOSEK (by typing type nchoosek in the MATLAB command window). Not only is a variable being grown inside a loop instead of being preallocated (as Amro pointed out in a comment), but the algorithm used is also recursive, meaning that many function calls are made. I also noticed this line at the end of the help text for NCHOOSEK:

    此语法仅适用于N小于约15的情况.

    This syntax is only practical for situations where N is less than about 15.

    这篇关于如何在向量的所有点之间绘制线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆