Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

function - how to generate a random binary matrix with a specific condition in matlab?

If matrix A shows the number of group of ones in G (n,m) matrix like that

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

so the A matrix will be

A = [ 2 1
      3 0
      1 3 ]

Then i want to generate (n,m) random matrix which the ones in this Matrix depend on A in the same order they appear

one solution will be

x = [ 0 1 1 0 1
      0 0 1 1 1
      1 0 1 1 1 ]

another solution

x = [ 1 1 0 1 0
      1 1 1 0 0
      1 0 1 1 1 ]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As mentions in comments to the OP, StackOverflow is not a code-writing service. Having said that, this was an interesting problem and I decided to make an exception and answer it anyway.

Formalities aside...


I think I have a general solution that can also handle some (all?) edge cases like G with zero rows etc. The code below generates one instance of the x matrix.

Creating an n-by-m array of these xs is left "as an exercise to the reader" (mainly because it's undefined whether OP wants it as a cell array of matrices or a 4-D logical/double array).

To understand what it does, see comments in the code + variable names. I hope it is clear enough (and that I didn't miss any edge cases).

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...