0

我创建了随机线,我想选择其中一些以将它们绘制为红色,而其余的将绘制为蓝色。

到目前为止我的代码是

%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

问题是它似乎不起作用。任何想法或建议都将受到欢迎!

4

1 回答 1

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) will plot a single element from X, see Matlab's linear indexing to understand why. X(:, i) uses subscript indexing with the colon operator : meaning all the rows in this case.

于 2013-04-09T14:23:12.867 回答