0

我使用以下代码创建了一个矩阵:

row=4;
column=5;
pop=5;


for ii = 1:(row*pop)
    done = false;
    while ~done
        newRow = randi([0 2],column,1);
        done = (sum(newRow)<12)...
            && (~any(diff([0;find(newRow);column+1])>3));
    end
    result(:,ii) = newRow;
end
result = permute(reshape(result,column,row,pop), [2 1 3]);

我得到一个矩阵结果(4,5,5),然后我想在特定行中再次随机并弹出由约束引起的。

for ii=1:pop;
       while size(find(waktu_libur(:,:,ii)>20&waktu_libur(:,:,ii)<23),1)~=11, 
       %#condition that had to be fullfilled by matrix result.
       for bb=1:row;
           if find(waktu_libur(bb,:,ii)>20&waktu_libur(bb,:,ii)<23),
               continue;
           else
           done = false;
           while ~done
                newRow = randi([0 2],column,1);
                done = (sum(newRow)<12)...
                && (~any(diff([0;find(newRow);column+1])>3));
           end
           dummy = newRow;
           dummy= permute(reshape(dummy,jum_bag,1), [2 1]); %#this matrix which is used to replace
           result(bb:,ii)=dummy %#process replacing dummy to row in matrix result that still obey contraint.
           end %#end looping if
        end %# end looping for
     end %#end looping while
end %# end looping for

我认为我的代码是正确的。但是运行后,它给出了这个错误:

??? Subscripted assignment dimension mismatch.

有谁看到导致此错误的原因?

4

1 回答 1

0

修复这些问题后,代码运行良好:

  1. 我换成waktu_liburresult
  2. 中缺少逗号result(bb:,ii)
  3. jum_bag中未定义reshape(dummy,jum_bag,1)。但是,由于reshape()要求输入和输出元素的数量相等,唯一的可能是设置jum_bag = numel(dummy).

在这些更改之后,代码运行。不幸的是,我无法重现您的错误。

但是,我建议更改其他一些内容:

  1. clear result在您的第一个代码片段的开头添加一个。您不初始化输出变量,而是动态构建它(顺便说一下,这很)。然后你改变形状result。如果您重新执行代码,动态创建将不起作用,因为result内存中已经存在尺寸错误的。

  2. 此代码newRow = randi([0 2],column,1);创建一个列向量。因此reshape(dummy,jum_bag,1),不需要以下也创建列向量的 。

于 2012-08-18T18:06:23.603 回答