MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   MATLAB从Loop绘制x,y (https://www.labfans.com/bbs/showthread.php?t=23674)

poster 2019-12-10 20:41

MATLAB从Loop绘制x,y
 
我不知道为什么这两个代码不会产生相同的情节?

代码1:

x=[2,5,7]; y=[10,4,3]; plot(x,y); 代码2:

x=[2,5,7]; y=[10,4,3]; for i=1:length(x) xx=x(i); yy=y(i); plot(xx,yy); end 编辑:我如何使Code 2的输出类似于Code 1输出



[B]回答:[/B]

[B]代码1 [/B]

您正在使用plot(x,y)命令用向量中定义的x和y坐标绘制一条[B]线[/B] 。默认情况下,它表示plot(x,y,'-');这表示您画一条线( - )。

[B]代码2 [/B]

由于要为每个点( xx , yy )调用plot ,因此您正在绘制存储在向量中的单个点(无线)。

您可以通过以下更改在代码1的代码2中复制效果:

plot(x,y,'.'); 这迫使MATLAB仅绘制[B]点[/B]而不是连接线

如果要点[B]和[/B]线,

plot(x,y,'-.'); 有关更多详细信息,请查看[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot.html"]plot[/URL]命令的文档。

[B]样例代码: [/B]

%# If you know the number of elements, preallocate %# else just initialize xx = []; and yy =[]; xx = zeros(100,1); yy = zeros(100,1); %# Get the data for i = 1:length(xx) xx(i) = getXSomehow(); yy(i) = getYSomehow(); end %# Plot the data plot(xx,yy);

[url=https://stackoverflow.com/questions/3219739]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:13

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.