How to plot all results from a for loop?

Hi all, I have created a function called maxrange and have created a for loop for this and am trying to plot the results. If I run the script it displays all the values for y that I want but still only plots the final value.
for m0=[73810:196.9:93500]
y=maxrange(m0)
end
x=linspace(10,19700,101)
plot(y,x)

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 4일
편집: Azzi Abdelmalek 2014년 12월 4일

0 개 추천

k=0;
t=73810:196.9:93500
y=zeros(size(t)) % pre-allocate
for m0=t
k=k+1;
y(k)=maxrange(m0)
end

댓글 수: 3

Hi, this seems to be working although could you briefly explain the line
y=zeros(sixe(t)) %pre-allocate
As I am unsure of what this is actually doing or what I should be doing.
Thanks a lot.
If the vector t=[1 2 3], the vector y should be the same size
y=zeros(size(t))
the result is
y=[0 0 0]
pre-allocating allows to make your loop faster, especially when the size of y is big
Jack
Jack 2014년 12월 4일
I see, Thanks very much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 12월 4일

댓글:

2014년 12월 4일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by