用基于索引的线连接两组点

Connect two set of points with a line based in the index

我想做的是用一条线连接两组点(基于 x,y)。该线应根据两组的索引绘制。意思是 set1(x,y) 应该连接到 set2(x,y),其中 xy 在两个集合中是相同的索引。

到目前为止我有以下内容:

set1 = [1,2; 3,4; 5,6];
set2 = [10,20; 30,40; 50,60];
plot(set1(:,1),set1(:,2),'b+',set2(:,1),set2(:,2),'g+')

显示 set1 蓝色点和 set2 绿色点的项目。意思是我想在 [1,2][10,20]

之间画一条线

是否有任何内置函数,或者我是否需要创建第三组代表线条,例如[ [1,2; 10,20], [3,4; 30,40], ... ]?

不需要构建函数,只要正确使用plot即可。如果您输入一个 x 值矩阵和一个 y 值矩阵,那么 plot 会将其解释为多个数据系列,其中每一列都是一个数据系列。

因此,如果您重新组织,您将设置为:

x = [set1(:,1) set2(:,1)].'
y = [set1(:,2) set2(:,2)].'

那么您只需输入:

plot(x,y)

带有我们数据的代码:

set1 = [1,2; 3,4; 5,6];
set2 = [10,20; 30,40; 50,60];
plot(set1(:,1),set1(:,2),'b+',set2(:,1),set2(:,2),'g+')
hold on
x = [set1(:,1) set2(:,1)].';
y = [set1(:,2) set2(:,2)].';
plot(x,y,'r')
hold off