How do I plot lines between all points in a vector?

后端 未结 2 681
灰色年华
灰色年华 2021-02-15 11:18

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 al

2条回答
  •  没有蜡笔的小新
    2021-02-15 11:55

    Building on gnovice's example, a simpler more intuitive way of generating all pairs is using the nchoosek function:

    %# random points
    N = 10;
    x = rand(1,N);
    y = rand(1,N);
    
    %# all possible combinations of the elements of [1:N] taken 2 at a time
    pairs = nchoosek(1:N, 2)';
    
    %'# plot points and lines
    plot(x(pairs), y(pairs), '-bs', 'MarkerFaceColor','g', 'MarkerSize',10)
    

    screenshot

提交回复
热议问题