Connecting line between different points obtained from a for-loop
이전 댓글 표시

Hello, I have obtained a plot of set of points from a for-loop as shown above. I simply want to add a line connecting these points. The function plot(x,y,'-*') does not work since these points are from different iterations. Please can someone help. Thanks.
댓글 수: 2
Colian Giannini
2016년 10월 12일
about this?
x=1:20;
y=zeros(length(x));
for i=1:length(x);
y(i)=x(i)+2;
end
plot(x,y,'-*')
AHN
2016년 10월 13일
답변 (4개)
Image Analyst
2016년 10월 14일
These work:
% Plot all at once:
subplot(2, 1, 1);
x = 1 : 20;
y = zeros(1, length(x));
for i = 1 : length(x)
y(i) = x(i) + 2;
end
plot(x,y,'b-*')
grid on;
% Plot a point at a time:
subplot(2, 1, 2);
grid on;
ylim([0, 22]);
xlim([0, 20]);
hold on;
x = 1 : 20;
y = zeros(1, length(x));
for i = 1 : length(x)
y(i) = x(i) + 2;
plot(x(1:i),y(1:i),'b-*')
drawnow;
pause(0.3);
end
Of course you could also vectorize and do away with the for loops
x = 1 : 20;
y = x + 2;
plot(x,y,'b-*')
grid on;
댓글 수: 4
AHN
2016년 11월 28일
Image Analyst
2016년 11월 28일
Can you at least give me some idea of what range of values z1 and z2 take on? Maybe just hard code 10 or 20 of the values from your program. Just print some out to the command window, copy, and paste into a line of code that will define z1 and z2.
AHN
2016년 11월 29일
AHN
2016년 11월 29일
SAKSHAM
2024년 10월 12일
0 개 추천
clc;
clear all;
x1=input("Enter the value of x1= ");
y1=input("Enter the value of y1= ");
x2=input("Enter the value of x2= ");
y2=input("Enter the value of y2= ");
dx=x2-x1;
dy=y2-y1;
k=0;
p=2*dy-dx;
plot(x1,y1,'o');
hold on
while x1<x2
if p<0
x1=x1+1;
p=p+(2*dy);
else
x1=x1+1;
y1=y1+1;
p=p+ 2*(dy-dx);
end
plot(x1,y1,'o');
end
grid on
please let me know how to connect the points with a line in this code
댓글 수: 1
DGM
2024년 10월 12일
Yet another reason to not write scripts that rely on ephemeral manual user input.
Are the initial points all scalar? If so, what's the point of the loop?
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!