I have the following code:
for x = 1:0.01:1.5
z = g(x,rpf);
end
but when I run this (assume my function is correct), I only get that z is equivalent to the last data point. How can I store x and z in a matrix and then plot them?

 채택된 답변

Bob Thompson
Bob Thompson 2019년 3월 5일

0 개 추천

You need to index z with each loop.
count = 0;
for x = 1:0.01:1.5;
count = count + 1;
z(count) = g(x,rpf);
end
plot(x,z)

댓글 수: 7

Benjamin
Benjamin 2019년 3월 5일
편집: Benjamin 2019년 3월 5일
This only has one value for x and z looks like it is produced as a row vector, not a column
Bob Thompson
Bob Thompson 2019년 3월 5일
Are you saying that z is a 2D array?
Plot should work no matter if x and z are row or column vectors, they just have to be 1D.
Benjamin
Benjamin 2019년 3월 5일
when i run this code, x is 1x1, not 51x1
Bob Thompson
Bob Thompson 2019년 3월 5일
편집: Bob Thompson 2019년 3월 5일
Mmm, I apologize. I get overzealous with my responses some times and miss things.
plot([1:0.01:1.5],z)
Benjamin
Benjamin 2019년 3월 5일
yeah, I mean I figured it out, I just set x=1:0.01:1.5 after the loop and before the plot command. was just noting that that plot command as it was could not explicitly be used. thanks for the help!
Benjamin
Benjamin 2019년 3월 5일
편집: Benjamin 2019년 3월 5일
Can I ask a follow-up? How could I do an outer loop for different values of rpf and then plot each one on the same plot? And the values for rpf would be like specific values: (i.e. 5.4, 6.7, 5.2 etc) Any ideas how I would do this? When I try it, it just keeps adding move rows to z, rather than just overwriting it on the next loop
Benjamin
Benjamin 2019년 3월 5일
I created a new question, maybe you could answer this question there.

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

추가 답변 (0개)

카테고리

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

질문:

2019년 3월 5일

댓글:

2019년 3월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by