用索引中的一条线连接两组点 [英] Connect two set of points with a line based in the index
问题描述
我想做的是用线连接两组点(基于x,y).应基于两组的索引绘制线.含义set1(x,y)
应该连接到set2(x,y)
,其中x
和y
是两组中相同的索引.
What I'm trying to do is connecting two sets of points (x,y based) with a line. The line should be drawn based on the index of the two sets. Meaning set1(x,y)
should be connected to set2(x,y)
where x
and y
are the same indices in both sets.
到目前为止,我的工作是:
What I have so far is the following:
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]
Displaying me the items of set1
in blue points and the set2
in green points. Meaning I want to plot a line between [1,2]
and [10,20]
是否有任何内置函数,或者是否需要创建代表行的第三组,例如[ [1,2; 10,20], [3,4; 30,40], ... ]
?
Is there any build-in function for this, or do I need to create a third set representing the lines e.g. [ [1,2; 10,20], [3,4; 30,40], ... ]
?
推荐答案
您不需要构建函数,只需正确使用plot
.如果输入x值矩阵和y值矩阵,则plot
会将其解释为多个数据系列,其中每一列都是一个数据系列.
You don't need to build a function, just to use plot
correctly. If you input a matrix of x values and a matrix of y values, then plot
interprets it as multiple series of data, where each column is a data series.
因此,如果您进行重组,则设置为:
So if you reorganize you sets to:
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
这篇关于用索引中的一条线连接两组点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!