No visible points for plot in for loop

依然范特西╮ 提交于 2021-02-17 06:19:37

问题


I'm struggling with a plot I want to make using a for-loop.

I know it works when I add it after the loop (just a simple plot). But I want to try it in this other way.

fib = ones(1:10);
for k=3:10
    hold on
    fib(k) = fib(k-1) + fib(k-2);
    plot(k,fib(k))
end
hold off

The output is a plot, but there are no points visible.


回答1:


You need to specify a marker. The documentation says:

If one of X or Y is a scalar and the other is either a scalar or a vector, then the plot function plots discrete points. However, to see the points you must specify a marker symbol, for example, plot(X,Y,'o')

So it will be:

plot(k,fib(k),'o');

Also note that you're creating a 10-dimensional array with fib = ones(1:10);. You most probably meant to write a comma instead of colon in between 1 and 10 to create a row vector. i.e.

fib = ones(1,10);

or a column vector as HansHirse suggested:

fib = ones(10,1);


来源:https://stackoverflow.com/questions/56166314/no-visible-points-for-plot-in-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!