如何解决错误“矩形空矩阵分配不当"? [英] How to solve error 'Improper assignment with rectangular empty matrix'?
问题描述
我写了这个凌乱的代码.当我使用功能daa
运行代码的第一部分时,有时会产生结果,但有时会产生错误消息.有谁知道如何纠正它?
I have written this messy code. When I run the first part of the code using the function daa
, some times it produces a result, but sometimes it produces an error message. Does anyone have some ideas how to correct it?
矩形空矩阵分配不当.
daa
中的错误(第26行)favoritec(i)=find(sPref(i,:)==bestmatch(i));
Improper assignment with rectangular empty matrix. Error in
daa
(line 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的定义如下:
Then the function daa is defined as following:
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
推荐答案
背景
我认为矩形空矩阵分配不当"意味着您试图将矩形空矩阵分配给标量位置.矩形空矩阵是显示为空矩阵:0乘1"的矩阵.生成这种矩阵的一种方法是对全为假的矩阵或本身为矩形的空矩阵进行find
.
在您的代码中,发生此错误是因为没有sPref(i,:)== bestmatch(i)的实例.
In your code, this error occurs because there were no instances where sPref(i,:)==bestmatch(i).
如果您输入
rng(1237)
然后执行您的代码(不清除所有内容).您可以重现该错误.
And then execute your code (without the clear all). You can reproduce the error.
查看在调用daa之前的变量,您可以看到daa([2, 1; 2, 1; 1, 2; 2, 1], [2, 1, 3, 4; 4, 2, 1, 3], [1;1])
失败.另一个断点显示sPref(1,:) = [2,NaN]
以及notProposed=[false, true]
,因此bestmatch(1)
是NaN
-永远不会等于任何东西.这表明该错误可能在于您在下一部分中如何将NaNs
分配给sPref
.
Looking at the variables just before daa is called, you can see that daa([2, 1; 2, 1; 1, 2; 2, 1], [2, 1, 3, 4; 4, 2, 1, 3], [1;1])
fails. Another breakpoint reveals that sPref(1,:) = [2,NaN]
and that notProposed=[false, true]
so bestmatch(1)
is NaN
- which is never equal to anything. This indicates that the bug likely lies in how you assign NaNs
to sPref
in the next section.
您需要自己找到该错误.但这应该可以回答您有关分配不当"错误的问题.
You'll need to find that bug yourself. But this should answer your question about the 'improper assignment' error.
-
此问题应标记为"matlab".用您遇到问题的主要工具或语言标记您的帖子,以便合适的人阅读.
This question should be tagged "matlab". Tag your post with the principal tool or language with which you are having problems so that the right people read it.
通过简短的复制步骤,可以轻松回答您的问题.尝试一些rng
的种子值并包含daa
函数调用要比使用随机数的大型步骤集要好.
A short set of steps to reproduce makes it much easier to answer your question. It would have been better to try a few seed values to rng
and include the daa
function call than the large set of steps with random numbers.
这篇关于如何解决错误“矩形空矩阵分配不当"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!