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
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
AHN 2016 年 10 月 13 日
Thank you for the reply. It's still not working on my problem, but good to know various approaches to the pseudo-code. I will keep trying, Thanks.

サインインしてコメントする。

回答 (4 件)

Pritesh Shah
Pritesh Shah 2016 年 10 月 12 日

0 投票

This will work for your case !!
All the best !!

1 件のコメント

AHN
AHN 2016 年 10 月 12 日
Thank you for the reply. I still cannot get a connecting line through the points. Here's my pseudo-code. Thanks.
for x=1:1:20;
y=x+2
hold all
plot(x,y,'*')
end

サインインしてコメントする。

Thorsten
Thorsten 2016 年 10 月 12 日

0 投票

x = 1:20;
y = x+2;
plot(x,y,'*-')

2 件のコメント

AHN
AHN 2016 年 10 月 13 日
Thank you for the reply. It is working on this code, but not working on my actual problem where I have used the for-loop with MCS. It says the matrix dimensions don't agree when I don't use for-loop.
Thorsten
Thorsten 2016 年 10 月 14 日
It would be helpful if you post a complete yet minimal example that does not work.

サインインしてコメントする。

Image Analyst
Image Analyst 2016 年 10 月 14 日

0 投票

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
AHN 2016 年 11 月 28 日
Thank you very much for the reply and sorry for a delayed response. Again, it doesn't work for my problem. I have added in a code, could you please check?
n=10;
x=z1+1;
y=z2+3;
for k=1:20;
t=(1-((k-x)./y));
count=0;
for j=1:n
if (t(j)<=0)
count=count+1;
end
end
m = zeros(1, length(k));
for i = 1 : length(m)
m(i) = count(i)/n;
hold all
plot(k,m,'b-*')
grid on;
end
end
z1 and z2 are dependent on other parameters which I have not added in this code. I am getting the points but need a connecting line between the points without changing the format of the first part of the code. Thank you very much.
Image Analyst
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
AHN 2016 年 11 月 29 日
Thank you. They are randomly generated integers; say between -2 and 2. Thank you very much for the help.
AHN
AHN 2016 年 11 月 29 日
z1 =
0.6510
0.6995
-0.2816
-0.1782
1.7098
0.1839
1.0352
-0.5937
0.3116
0.2233
z2 =
1.7924
-1.3664
0.0013
0.0541
-1.3397
1.3086
1.1972
-0.1535
0.7780
-1.0430

サインインしてコメントする。

SAKSHAM
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
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 ExchangeMatrix Indexing についてさらに検索

質問済み:

AHN
2016 年 10 月 12 日

コメント済み:

DGM
2024 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by