2010-07-17 32 views
1

我需要幫助繪製點之間的線條。 想,我開始創建6個隨機點 -需要幫助繪製點之間的線條

x = rand(6,1); 
y = rand(6,1); 

所以我的觀點是(x(1),y(1)), (x(2),y(2)), (x(3),y(3)), (x(4),y(4)), (x(5),y(5)), (x(6),y(6))

現在我想畫點1 & 5,2 & 6之間的直線,3 和將它們繪製在一張圖中。所以我得到3條直線。

任何幫助將不勝感激。

+0

感謝名單一大家很多...我的問題是解決了 – Faisal 2010-07-18 14:56:42

回答

1

這裏有兩種方法可以做到這一點:

第一種方式,用hold on。這些線是分開的,即如果您將其中一個紅色,其他紅色將保持藍色。

%# plot the first line 
plot([x(1);x(5)],[y(1);y(5)]); 

hold on %# this will prevent the previous plot from disappearing 

%# plot the rest 
plot([x(2);x(6)],[y(2);y(6)]); 
plot([x(3);x(4)],[y(3);y(4)]); 

第二種方式,利用事實NaN沒有得到繪製。這些線條組合在一起,即如果您打開一個紅色線條,所有線條都會變成紅色。

%# create array for plotting 
xy = NaN(8,2); 

%# fill in data 
xy([1 2 4 5 7 8],1) = x([1 5 2 6 3 4]); 
xy([1 2 4 5 7 8],2) = y([1 5 2 6 3 4]); 

%# plot 
plot(xy(:,1),xy(:,2)) 
2

您可以通過致電PLOT一次。如果你重塑你的xy數據與含有一組座標一條線每列的矩陣,那麼PLOT將以此爲每一列不同顏色的線:

index = [1 2 3; 5 6 4]; %# The index to reshape x and y into 2-by-3 matrices 
plot(x(index),y(index)); %# Plot the lines