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 に答える