2013-04-09 172 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,冒号运算符:表示这种情况下的所有行。

相关问题