separate lines on 2d graph using two separate pairs of vectors

How do draw separate lines, one from x1, y1 to m1, n1, another from x2, y2 to m2, n2 and so on...?
Thanks so much!

Answers (2)

line([x1,m1],[y1,n1]);
hold on;
line([x2,m2],[y2,n2]);

2 Comments

hold on is unnecessary in this case.
Correct, Grzegorz.
For future reference by other people reading this: line() always adds to the current graphics, no matter what the hold state is. plot(), however, pays attention to the hold state.

Sign in to comment.

% generate some data
x1 = rand();
y1 = rand();
m1 = rand();
n1 = rand();
x2 = rand();
y2 = rand();
m2 = rand();
n2 = rand();
% option one
figure
line([x1 m1],[y1 n1],'color','b')
line([x2 m2],[y2 n2],'color',[0 0.498 0])
% option 2
figure
plot([x1 m1],[y1 n1],[x2 m2],[y2 n2])
% option 3
figure
hold all
plot([x1 m1],[y1 n1])
plot([x2 m2],[y2 n2])

3 Comments

Thanks! So I guess that means that for long vectors I should do it with a for loop.
for i - 1:length(x)
figure
line([x(i) m(i)],[y(i) n(i)])
end
Thanks again!
line( reshape([x(:),m(:),nan(numel(x),1)].',1,[]), ...
reshape([y(:),n(:),nan(numel(x),1)].',1,[]) );
Brilliant, Walter! Thank you so much!!

Sign in to comment.

Tags

Asked:

on 26 Sep 2011

Community Treasure Hunt

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

Start Hunting!