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 x
s 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