我是matlab的初学者.现在我想要一个循环,这样它就可以迭代一个向量(不是连续的数字),它通过迭代包含数量减少的元素.
例如,我[1; 2 ;3; 4](将其视为1,2,3,4号人物)然后我想做一些事情,例如第1和第4人获得食物,第2和第3人没有食物.
在下一轮中,我希望第2和第3人(未分配的人)再次完成分配过程但不是1和4.所以我创建了一个向量[2; 3]以跟踪那些没有食物的人.
然而,for i=1:length(vector)给我一系列连续的数字,我想要的是
for i in vector do something; end
怎么实现这个?
当我刚刚放
i=vector,
Run Code Online (Sandbox Code Playgroud)
Matlab说Index超过了矩阵维度
我写了这个凌乱的代码.当我使用该函数运行代码的第一部分时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)
Run Code Online (Sandbox Code Playgroud)
然后函数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 …Run Code Online (Sandbox Code Playgroud)