0

ランダムな開始点と終了点を作成しています。原点に配置された長方形と交差/交差する人をプロットしたい。図に示すように、コードにいくつかの行が欠けていることがわかりました。その後、トラックが長方形にぶつかったかどうかを数えたいと思います。たとえば、トラックは上から来て、右側から出ました。

私のコードは

function [hot, cold, h] = MuonTracks(tracks)%#eml

    % NOTE: no variables larger than 1x1 are initialized

    width  = 1e-4;
    height = 2e-4;

    % constant used for Laplacian noise distribution  
    bL = 15 / sqrt(2);

    % Loop through all tracks
    X = [];
    bhit= [];
    hot = 0;  
    ii = 0;
    TopBottom= 0;
    LeftRight= 0;
    TopRight= 0;
    TopLeft= 0;
    LeftBottom= 0;
    RightBottom= 0;
    ihit= 0;
    while ii <= tracks

        ii = ii + 1;

        % Note that I've inlined (== copy-pasted) the original laprnd()
        % function call. This was necessary to work around limitations 
        % in loops in Matlab, and prevent the nececessity of those HUGE 
        % variables. 
        %
        % Of course, you can still easily generalize all of this: 

        % the new data
        u = rand-0.5;

        Ystart = 15; 
        Xstart = 80*rand-40;
        Xend   = Xstart - bL*sign(u)*log(1-2*abs(u));
        %Xend=laprnd(tracks,1,Xstart,15);

        b = (Ystart*Xend)/(Xend-Xstart);


        % the test
        if ((b < height && b > 0)) ||...
            (Xend < width/2 && Xend > -width/2)

            hot = hot+1;

            % growing an array is perfectly fine when the chances of it
            % happening are so slim
            X = [X [Xstart; Xend]]; %#ok
            bhit=b;

        end
    end

    % This is trivial to do here, and prevents an 'else' 
    cold = tracks - hot;

    % Now plot the chosen ones
    h = figure;
    hold all    
    %Y = bsxfun(@times, 15, ones(size(X)));
    if (size(X)==0)
        %Disp('No hits were found');
        msgbox('No tracks were found','Result','warn');
    elseif (size(X)>1)
         Y = bsxfun(@times, [15; 0], ones(size(X)));
        plot(X, Y, 'r');
        msgbox( [num2str(hot) ' tracks were found'],'Result','help',num2str(hot));
    else
        Y = bsxfun(@times, [15; 0], ones(size(X)));
        plot(X, Y, 'r');
        msgbox( [num2str(hot) ' track was found'],'Result','help',num2str(hot));
    end
    %X;
    %Y;
    %size(X,2)
    while ihit<size(X,2)

        ihit=ihit+1
        %X(2,ihit)

        if ((X(2,ihit)==-width && (bhit<=0 && bhit<=height))&&(bhit==0 && (X(2,ihit)>=-width && X(2,ihit)>=width)))
            LeftBottom=LeftBottom+1;
        elseif ((bhit==height && (X(2,ihit)>=-width && X(2,ihit)>=width)) && (X(2,ihit)==width && (bhit<=0 && bhit<=height)))
            TopRight=TopRight+1;
        elseif ((bhit==height && (X(2,ihit)>=-width && X(2,ihit)>=width)) && (bhit==0 && (X(2,ihit)>=-width && X(2,ihit)>=width)))
            TopBottom=TopBottom+1;
        elseif ((X(2,ihit)==-width && (bhit<=0 && bhit<=height)) && (X(2,ihit)==width && (bhit<=0 && bhit<=height)))
            LeftRight=LeftRight+1;
        elseif ((X(2,ihit)==-width && (bhit<=0 && bhit<=height)) && (bhit==height && (X(2,ihit)>=-width && X(2,ihit)>=width)))
            TopLeft=TopLeft+1;
        elseif ((X(2,ihit)==width && (bhit<=0 && bhit<=height)) && (bhit==0 && (X(2,ihit)>=-width && X(2,ihit)>=width)))
            RightBottom=RightBottom+1;
        else
            display('sth is wrong');
        end
        X(2,ihit)
    end
    %X(1,:);
    %Y(1,:);
    LeftBottom
    TopRight
    TopBottom
    LeftRight
    TopLeft
    RightBottom
    %display('sdfghjk');
end

どんなアイデアでも大歓迎です!

4

1 に答える 1

0

ここには、セグメントのグループ全体を交差させ、交点を返すことができる関数があります。お役に立てば幸いです。

于 2013-04-14T15:45:34.600 に答える