very strange error of for loop
2 次查看(过去 30 天)
显示 更早的评论
I made this kind structure code
but the result is not I had expecetd
k=1;
for i=1:1:100
if(i==1)
a=zeros(size(i));
b=zeros(size(i));
end
a(k)=sin(i);
b(k)=a(k)
k=k+1
end
a(k) display the sin wave
but b(k) dispay only one constant value
like y=3
what happend to my code?
0 个评论
采纳的回答
Star Strider
2020-4-16
You did not include the plot call you are using.
If you are plotting:
figure
plot(a,b)
it will show a straight line from (-1,-1) to (1,1) because both ‘a’ and ‘b’ have the same values.
.
2 个评论
Star Strider
2020-4-16
I am using R2020a, and Windows 10.
Is that the only code you are running in that script?
There could be version differences, however I doubt they would be that extreme.
If you run this line:
which plot -all
from your Command Window or a script, all the results should share this part of the path to each (the exact configuration could be operating-system dependent):
C:\Program Files\MATLAB\R2020a\toolbox\matlab\
The rest oif the path will likely vary. If any do not, and it turns out that you have another function named ‘plot’, that could be the problem. (Naming user-defined functions the same as MATLAB built-in functions is called ‘overshadowing’ and is to be avoided.)
Beyond that, I have no idea what the problem could be.
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!