# Why does my plot not display when I use a nested loop?

조회 수: 2 (최근 30일)
Mal 2015년 5월 28일
댓글: Mal 2015년 5월 28일
I am trying to use the following code to iteratively plot various lines on a single graph:
hold on
for b=1:1:4
for a=0:4:16
c=a+24*b;
plot (a, c)
end
end
hold off
Why is my figure blank when I run it?

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

### 채택된 답변

편집: Michael Haderlein 2015년 5월 28일
If you want to get this as line plot, you'll need all values of a and c to be in one array each. If the example you have posted is the real equation, you should simply vectorize it and things become much easier:
b=1:1:4;
a=0:4:16;
[Am,Bm]=meshgrid(a,b);
Cm=Am+24*Bm;
plot(a,Cm)
If this was just sketching the problem and you cannot vectorize your function, you'll need to save all c values:
b=1:1:4;
a=0:4:16;
c=zeros(numel(a),numel(b));
for cntb=1:numel(b)
for cnta=1:numel(a)
c(cnta,cntb)=a(cnta)+24*b(cntb);
end
end
plot(a,c)
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Mal 2015년 5월 28일
Thank you! I get the plot I want by vectorizing the function.

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

### 카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by