2013-04-09 64 views
0

我已經創建了隨機線,並且我想選擇其中的一些以便將它們繪製成紅色,而其餘的將繪製成藍色。選擇特定數組並在MatLab中創建並繪製一個新數組

到目前爲止我的代碼是

%Initial line values 

tracks=input('Give me the number of muon tracks: '); 
width=20; 
height=5; 

Ystart=15.*ones(tracks,1); 
Xstart=-80+160.*rand(tracks,1); 
Xend=-80+160.*rand(tracks,1); 
X=[Xstart';Xend']; 
Y=[Ystart';zeros(1,tracks)]; 
b=(Ystart.*Xend)./(Xend-Xstart); 

for i=1:tracks 
    if ((Xend(i,1)<width/2 && Xend(i,1)>-width/2)||(b(i,1)<height && b(i,1)>0)) 
     plot(X(:, i),Y(:, i),'r');%the chosen ones! 
     hold all 
    else 
     plot(X(i),Y(i),'b');%the rest of them 
     hold all 
    end 
end 

輸出是

一個想法是創建新載體,並嘗試繪製出來。我的嘗試如下

%Initial line values 

tracks=input('Give me the number of muon tracks: '); 
width=20; 
height=5; 

Ystart=15.*ones(tracks,1); 
Xstart=-80+160.*rand(tracks,1); 
Xend=-80+160.*rand(tracks,1); 
X=[Xstart';Xend']; 
Y=[Ystart';zeros(1,tracks)]; 
b=(Ystart.*Xend)./(Xend-Xstart); 

countHot=0; 
countCold=0; 

for i=1:tracks 
    if ((Xend(i,1)<width/2 && Xend(i,1)>-width/2)||(b(i,1)<height && b(i,1)>0)) 
     countHot=countHot+1; 
    else 
     countCold=countCold+1; 
    end 
end 

Xhot=zeros(countHot,1); 
Yhot=zeros(countHot,1); 
Xcold=zeros(countCold,1); 
Ycold=zeros(countCold,1); 

for i=1:tracks 
    if ((Xend(i,1)<width/2 && Xend(i,1)>-width/2)||(b(i,1)<height && b(i,1)>0)) 
     Xhotend(i)=Xend(i); 
     Xhotstart(i)=Xstart(i); 
     Yhotend(i)=Yend(i); 
     Yhotstart(i)=Ystart(i); 
    else 
     Xcoldend (i)=Xend(i); 
     Xcoldstart(i)=Xstart(i); 
    end 
end 

事情是,它似乎並沒有工作。任何想法或建議都會比歡迎!

+0

我想在你的第一個代碼中嘗試'plot(X(:,i),Y(:,i),'r');%所選擇的那個!' – Dan 2013-04-09 13:57:44

+0

@Dan:非常感謝您的評論!這種替代方法你建議只繪製紅線並忽略邏輯操作。我將編輯我的問題,使用你的繪圖並添加輸出。 – Thanos 2013-04-09 14:04:34

+0

也許我應該改變其他的。這是工作!!!你介意發佈它作爲答案,以便我可以接受它嗎?你提供的代碼是做什麼的? – Thanos 2013-04-09 14:13:15

回答

1

你的第一個代碼幾乎是正確的,你只需要從XY,而不僅僅是簡單的單點繪製列:

%Initial line values 

tracks=input('Give me the number of muon tracks: '); 
width=20; 
height=5; 

Ystart=15.*ones(tracks,1); 
Xstart=-80+160.*rand(tracks,1); 
Xend=-80+160.*rand(tracks,1); 
X=[Xstart';Xend']; 
Y=[Ystart';zeros(1,tracks)]; 
b=(Ystart.*Xend)./(Xend-Xstart); 

for i=1:tracks 
    if ((Xend(i,1)<width/2 && Xend(i,1)>-width/2)||(b(i,1)<height && b(i,1)>0)) 
     plot(X(:, i),Y(:, i),'r');%the chosen ones! 
     hold all 
    else 
     plot(X(:,i),Y(:,i),'b');%the rest of them 
     hold all 
    end 
end 

X(i)將從X繪製一個單一的元素,見Matlab's linear indexing理解爲什麼。 X(:, i)使用subscript indexing,冒號運算符:表示這種情況下的所有行。

相關問題