Early-terminating algorithm?

56 views
Skip to first unread message

John McCaw

unread,
Jan 18, 2015, 2:12:38 AM1/18/15
to fre...@googlegroups.com
Hello all, 
I've been working on a short function for the Gale-Shapely Algorithm to match pairs by preference in a stable configuration, and it's working fine for two groups of 2 and two groups of 3. However, whenever I input N=4 or more, I get "Error: index values must be >=1" after the first cycle. So you can see what I'm talking about, below is my project. The error is coming up at " if mstatus(A,1)==0 & wstatus(menp(A,B),1)==0" the second time through. I'm wondering if something I've missed is causing it to terminate early.

function match = match(N, menp, womenp)  % here N is the number of each group and menp and womenp are N by N matrices of their preference order, listed 1 to N.


     engaged = zeros(N,N); % Where 1 is a true engagement and 0 is false for men and women, each row is a woman and each column is a man.


     mstatus = zeros(N,1); % The status of each man, either 1 (engaged) or 0 (not engaged).


     wstatus = zeros(N,1); % The status of each woman, either 1 (engaged) or 0 (not engaged).


 for B=[1:N]

     for A=[1:N]

       if mstatus(A,1)==0 & wstatus(menp(A,B),1)==0

                engaged(menp(A,B),A)=1;

                wstatus(menp(A,B),1)=1;

                mstatus(A,1)=1;

       elseif womenp(menp(A,B),A) < womenp(menp(A,B), engaged(womenp(A,B), find(engaged(:,menp(A,B))==1)))

                mstatus(find(engaged(menp(A,B),:)==1),1)=0;

                engaged(menp(A,B),:)=0;

                engaged(menp(A,B),A)=1;

                wstatus(menp(A,B),1)=1;

                mstatus(A,1)=1;

       else womenp(menp(A,B),A) > womenp(menp(A,B), engaged(womenp(A,B), find(engaged(:,menp(A,B))==1)))

               wstatus(menp(A,B),1)=1;

               engaged(menp(A,B),A)=0;

               mstatus(A,1)==0;

        end

end

end


match = engaged


Thanks for any advice you can provide!

Reply all
Reply to author
Forward
0 new messages