私はこの厄介なコードを書きました。function を使用してコードの最初の部分を実行するとdaa
、結果が生成されることもありますが、エラー メッセージが生成されることもあります。誰かがそれを修正する方法についていくつかのアイデアを持っていますか?
長方形の空行列の割り当てが不適切です。エラー
daa
(26 行目)favoritec(i)=find(sPref(i,:)==bestmatch(i));
n
との次元を増やしたとき、それは特に問題ですm
これがコードです
clear all;
n=4;
m=2;
Q=[1;1];
sPref=zeros(n,m);
cPref=zeros(m,n);
for i=1:n
sPref(i,:)=randperm(m);
end
for j=1:m
cPref(j,:)=randperm(n);
end
match=daa(sPref,cPref,Q)
次に、関数 daa は次のように定義されます。
function match=daa(sPref,cPref,Q)
proposition=false(size(sPref)); % keep track who has proposed to which. False= not proposed yet
match=zeros(length(sPref),1);
favoritec=zeros(length(sPref),1);
numberS=zeros(size(cPref,1),1);
bestmatch=zeros(length(sPref),1);
iter=0;
while (min(match)==0) % as long as there is one unmatched, continues the loop (except the break)
iter=iter+1;
app=find(match==0);
for i=app(1:end)'
notProposed=(proposition(i,:)==false);
bestmatch(i)=min(sPref(i,notProposed));
favoritec(i)=find(sPref(i,:)==bestmatch(i));
numberS(favoritec(i))= numberS(favoritec(i))+1; % keep track of the no.of applicants
proposition(i,bestmatch(i))=1; % propsed to college j finishes,either reject or accept
end
% college deciding...
for j=1:size(cPref,1)
S_comp=find(favoritec==j); % find the students competing for the same Favoritec
if numberS(j) <=Q(j) % sum of students at the college smaller or equal than quota
match(S_comp)=favoritec(S_comp); % accept tentative offer
numberS(j)=sum(match==j);
sPref(S_comp,j)=NaN;
else
noapl=setxor(1:length(cPref),S_comp);
cPreft=cPref(j,:); % truncated pref,change the pref of those who didn't apply to NaN
cPreft(noapl)=NaN;
[r,I]=sort(cPreft);
topq=I(1:Q(j)); % college takes the top quota q students
match(S_comp)=0; % clean the previous assignment
match(topq)= favoritec(topq);
numberS(favoritec)=Q(j);
rejapp=setxor(S_comp,topq); % the students who got rejected
sPref(rejapp,j)=NaN;
end
%display(match);
end
%% if all choices have proposed, then stop
if proposition(i,:)==true;
display('already proposed to every college')
display(i)
break
end
end