MATLAB: игнорирование определенных столбцов в матрице при выполнении цикла - PullRequest
2 голосов
/ 19 октября 2011

пока у меня есть это:

for l=1:50
    %# matrix of distances between terminal nodes, index of column represents 
    %# where the searcher is going from and the index of the row represents 
    %# where the searcher is going to
    d = [
    10  2 2 3 3 2 4 5 5 4 4 4 3 3 4 4;
    10 10 2 5 5 4 6 7 7 6 6 6 5 5 6 6;
    10 2 10 5 5 4 6 7 7 6 6 6 5 5 6 6;
    10 5 5 10 2 3 7 8 8 7 7 7 6 6 7 7;    
    10 5 5 2 10 3 7 8 8 7 7 7 6 6 7 7;    
    10 4 4 3 3 10 6 7 7 6 6 6 5 5 6 6;    
    10 6 6 7 7 6 10 3 3 2 4 4 5 5 6 6;    
    10 7 7 8 8 7 3 10 2 3 5 5 6 6 7 7;    
    10 7 7 8 8 7 3 2 10 3 5 5 6 6 7 7;
    10 6 6 7 7 6 2 3 3 10 4 4 5 5 6 6;
    10 6 6 7 7 6 4 5 5 4 10 2 5 5 6 6;
    10 6 6 7 7 6 4 5 5 4 2 10 5 5 6 6;
    10 5 5 6 6 5 5 6 6 5 5 5 10 2 3 3;
    10 5 5 6 6 5 5 6 6 5 5 5 2 10 3 3;
    10 6 6 7 7 6 6 7 7 6 6 6 3 3 10 2;
    10 6 6 7 7 6 6 7 7 6 6 6 3 3 2 10
    ];

    i=1;                         %# start the searcher at the origin    
    h=5;    
    t=0;                         %# start time of the game
    k=find(d(i,:)==min(d(i,:))); %# position closest nodes
    j=randsample(k,1,true);      %# randomly selects closest node if multiple nodes within closest distance
    c=min(d(i,:));

    while j~=h       %# while the searcher is not in the same position as the hider
        d(:,j)=[];   %# delete the column corresponding to the searchers position so he cannot return here

        %# want to ignore all previous positions not delete them!        
        i=j;                        %# reset the searchers current position        
        k=find(d(i,:)==min(d(i,:))) %# find the new minimum distance        
        p=rand(1)*length(k);        %# randomly select an index of k        
        q=floor(p)+1                %# take the integer part of k        
        j=k(q)                      %# randomly select from all the closest nodes        
        t=t+min(d(i,:))+c;          %# calculate cumulative time for paths traveled
    end

    Found(l)=j-1 %# show position where the hider was found (relate matrix index to node index)
    Time(l)=t    %# show time taken for the searcher to find the hider
end

Таким образом, элементы в d - это расстояние между двумя позициями, представленное позицией строки и столбца, тогда цель состоит в том, чтобы достичь столбца 5 (h), пройдя кратчайшие расстояния между позициями.

Проблема, с которой я столкнулся, заключается в том, что в настоящее время я удаляю каждый столбец, так как выполняется поиск минимального расстояния, затем производится переопределение индекса столбца, и поэтомуh либо не достигнут, либо неверен h.Как можно игнорировать строки, уже найденные в цикле while?

Любая помощь будет с благодарностью получена, спасибо.

1 Ответ

0 голосов
/ 19 октября 2011

Вы можете создать массив flags=zeros(16,1) и включить flag(i)=1 при посещении строки. Затем вы можете проверить, помечена ли строка.

...