在 MATLAB 中将直线拟合为二维点
我尝试计算一条可以在 MATLAB 中用二维坐标拟合给定多个点的线。但结果却不是我所期望的。可能有什么我理解错误的地方。有人可以帮我吗?多谢。代码如下:
ptsAroundVCP_L=[180,188;177,191;174,191;171,191;168,188;] % points with 2-d coordinate
curLinePar_L=polyfit(ptsAroundVCP_L(:,2),ptsAroundVCP_L(:,1),1); % parameter of the fitted line
%% begin to plot
plotx=1:256;
figure(11);hold on;
plot(ptsAroundVCP_L(:,2),ptsAroundVCP_L(:,1),'sb');
ploty_L=polyval(curLinePar_L,plotx);
plot(plotx,ploty_L,'r');
hold off;
输出结果如下。但我期望的是,在这种情况下,拟合线应该垂直。我认为线路安装有问题。
I try to calculate a line that can fit given several points with 2-d coordinate in MATLAB. But the result is not I expected. There may be something I understand wrong. Can anyone help me out? Thanks a lot. The code is as follows:
ptsAroundVCP_L=[180,188;177,191;174,191;171,191;168,188;] % points with 2-d coordinate
curLinePar_L=polyfit(ptsAroundVCP_L(:,2),ptsAroundVCP_L(:,1),1); % parameter of the fitted line
%% begin to plot
plotx=1:256;
figure(11);hold on;
plot(ptsAroundVCP_L(:,2),ptsAroundVCP_L(:,1),'sb');
ploty_L=polyval(curLinePar_L,plotx);
plot(plotx,ploty_L,'r');
hold off;
The output is shown as follows. But what I expected is that the fitted line should go vertically in this case. I think there is something wrong with the line fitting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给定的数据不可能拟合任何合理的多项式:
进行转置,您将得到合理的结果:
It is impossible to fit any reasonable polynomial to this data as given:
Take the transpose and you will get something reasonable:
我认为问题基本上是你无法以斜率截距形式表示垂直线。如果你根据自己的需要翻转 x/y,你会得到正确的结果:
I think the problem is basically that you can't represent a vertical line in slope-intercept form. If you flip x/y in your fit, you get the right result: