2016-05-05 10 views
-1

行列Aは、そのどのようにmatlabで特定の条件でランダムなバイナリ行列を生成するには?

G = [ 1 1 0 0 1 
     0 1 1 1 0 
     1 0 1 1 1 ] 

などGにおける1のグループの数(N、M)行列を示しているので、もしマトリックスが

A = [ 2 1 
     3 0 
     1 3 ] 

になり、私は、N(生成しますM)、このマトリックス中のものは、彼らが

一つの解決策は

x = [ 0 1 1 0 1 
     0 0 1 1 1 
     1 0 1 1 1 ] 
0123あろう現れるのと同じ順序でAに依存するランダムマトリックス

別ソリューション

x = [ 1 1 0 1 0 
     1 1 1 0 0 
     1 0 1 1 1 ] 
+2

これはなぜRタグを持っていますか? – lmo

+2

入力とは何ですか? Wharは望ましい出力ですか?これまでに何を試しましたか? – Crowley

+0

@lmo同じ理由で 'matlab'タグがあるかもしれません - R、Matlab、Octaveは行列を扱うのによく使われます。 – Crowley

答えて

1

OPへのコメントで述べているように、StackOverflowのは、コード・ライティングのサービスではありません。これは面白い問題だったと私は例外を作り、とにかくそれに答えることにしました。余談

手続き...


私はまた、以下のコードはxの1つのインスタンスを生成などゼロの行とGのようないくつかの(すべて?)のエッジケースを扱うことができる一般的な解決策があると思いますマトリックス。

これらx Sのn行列mアレイを作成することがOPは、行列のセル配列または4-Dの論理的/二重アレイとしてそれを望んでいるかどうかを未定義だ主な理由(「読者への課題として」残され)。

コードと変数名のコメントを参照してください。私はそれが十分明確であることを望む(そして、私はどんな縁のケースも見逃さなかった)。

function x = q37055681(G) 
    %% Input 
    if nargin < 1 
    G = [ 1 1 0 0 1 
      0 1 1 1 0 
      1 0 1 1 1 ]; 
    end 
    %% Input analysis: 
    [A_ROWS,OUT_COLS] = size(G); 
    transitions = find(diff(padarray(G.',1,false,'both').',1,2).'); 
    tr_per_ln = hist(ceil(transitions/(size(G,2)+1)),size(G,1))/2; 
    A_COLS = max(tr_per_ln); 
    missing_trans_per_line = A_COLS - tr_per_ln; 
    groups_of_ones = diff(reshape(transitions,2,[]),1,1); % < result of RLE which ignores 0's 
    % Count "fixing" based on the external definition of what to do with only 1 group per 
    % line (in this case, count it in the first element of A): 
    insrt = @(what, into, where) cat(2, into(1:where-1), what, into(where:end)); 
    for indZ = 1:sum(missing_trans_per_line(:)) 
    next_incomplete = find(missing_trans_per_line,1); 
    groups_of_ones = insrt(0, groups_of_ones, A_COLS*next_incomplete-... 
           (missing_trans_per_line(next_incomplete)-1)); 
    missing_trans_per_line(next_incomplete) = missing_trans_per_line(next_incomplete)-1; 
    end 
    A = reshape(groups_of_ones,A_COLS,[]).'; 
    %% Generation of output: 
    x = zeros(size(G)); 
    for indR = 1:A_ROWS 
    tokens = cell(sum(A(indR,:)~=0),1); 
    switch numel(tokens) 
     case 0 
     % we don't need to do anything, the entire line is 0. 
     continue; 
     case 1 
     tokens{1} = repelem(true,A(indR,1)); 
     otherwise 
     for indT = 1:numel(tokens)-1 
      tokens{indT} = [repelem(true,A(indR,indT)) 0]; 
     end 
     tokens{end} = repelem(true,A(indR,find(A(indR,:),1,'last'))); 
    end 
    zero_tokens = repmat({0},[OUT_COLS-sum(A(indR,:))-(numel(tokens)-1),1]); 
    % Now we need to build a vector that selects randomly but SEQUENTIALLY from 
    % EITHER tokens or zero_tokens. 
    cell_to_pick_from = [ones(1,numel(tokens)) zeros(1,numel(zero_tokens))]; 
    choosing_order = cell_to_pick_from(randperm(numel(cell_to_pick_from))); 
    %^Here's where the randomness comes in: 
    x_line = []; 
    for indC = 1:numel(choosing_order) 
     if choosing_order(indC) 
     % if it's 1, choose from "tokens" 
     token = tokens{sum(choosing_order(1:indC)==1)}; 
     else 
     % if it's 0, choose from "zeros" 
     token = zero_tokens{sum(choosing_order(1:indC)==0)}; 
     end 
     x_line = [x_line token]; %#ok 
    end 
    x(indR,:) = x_line; 
    end 
end 
関連する問題