0 和 1 的矩阵,其中后续行中的分配取决于前一行

Matrix of 0s and 1s Where Assignment in Subsequent Rows are Contingent on the Previous Row

我想在 MATLAB 中创建一个矩阵,其中:

第一行由随机排列的 0 和 1 组成,均匀分布(即 50-50)。

第二行将第一行中 50% 的 0 和 1 随机分配为 0,将 1 分配给剩余的 50%。

第三行将第二行中 50% 的 0 和 1 随机分配为 0,将 1 分配给剩余的 50%。

非随机示例:

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1  
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1  
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1

有什么建议吗?

使用randperm的解决方案:

nrows = 3;
ncols = 16;
M = zeros(nrows,ncols);    

%// seed the first row
M(1,1:ncols/2) = 1;
M(1,:) = M(1,randperm(ncols));

for r = 2:nrows

    %// Find ncols/4 random between 1 and ncols/2. These will be used to index half of the previous rows 1 elements and set them to one
    idx = randperm(ncols/2);
    idx1 =  idx(1:ncols/4);
    %// Do the same thing again, but this time it will be used for the 0 elements of the previous row
    idx = randperm(ncols/2);
    idx0 =  idx(1:ncols/4);

    idx_prev1 = find(M(r-1,:)); %// Find where the 1 elements were in the last row
    idx_prev0 = find(~M(r-1,:)); %// Find where the 0 elements were in the last row   

    M(r,idx_prev1(idx1))=1; %// Set half of the previous rows 1 elements in this row to 1
    M(r,idx_prev0(idx0))=1; %// Set half of the previous rows 0 elements in this row to 1

end

基于检查数字是否大于或小于中位数的解决方案。只要测试的列数是偶数,一组随机双打中恰好有一半会大于中位数,一半会更小。这保证了恰好有 50% 的位被翻转。

nRows = 3;
nCols = 16; %# divisible by 4

%# seed the array
%# assume that the numbers in each row are unique (very, very likely)
array = rand(nRows,nCols); 


out = false(nRows,nCols);

%# first row is special
out(1,:) = array(1,:) > median(array(1,:));

%# for the rest of the row, check median for the zeros/ones in the previous row
for iRow = 2:nRows
    zeroIdx = out(iRow-1,:) == 0;
    %# > or < do not matter, both will replace zeros/ones 
    %# and replace with exactly half zeros and half ones
    out(iRow,zeroIdx) = array(iRow,zeroIdx) > median(array(iRow,zeroIdx));
    out(iRow,~zeroIdx) = array(iRow,~zeroIdx) > median(array(iRow,~zeroIdx));
end

我会提供一个简短的 bsxfun 解决方案:

%// number of divisions
n = 4;

%// unshuffled matrix like in your example
unshuffled = bsxfun(@(a,b) mod(a,2*b) > b-1, meshgrid(1:n^2,1:n) - 1, (2.^((n-1):-1:0)).') %'

%// shuffle columns
shuffled = unshuffled(:,randperm(n^2))

unshuffled =

 0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
 0     0     0     0     1     1     1     1     0     0     0     0     1     1     1     1
 0     0     1     1     0     0     1     1     0     0     1     1     0     0     1     1
 0     1     0     1     0     1     0     1     0     1     0     1     0     1     0     1


shuffled =

 1     0     1     1     0     1     0     1     1     1     1     0     0     0     0     0
 1     1     1     0     0     1     1     0     1     0     0     0     1     0     1     0
 1     0     0     1     0     0     0     0     1     1     0     1     1     0     1     1
 1     1     1     1     0     0     0     0     0     0     1     0     0     1     1     1

首先您需要创建未打乱的矩阵,这可以通过将 meshgrid(1:n^2,1:n) 生成的矩阵与行相关模数进行比较来完成。最后你只需要洗牌。

如果你有 Statistics Toolbox,你可以很容易地用 randsample:

M = 3; %// number of rows
N = 16; %// number of columns. Should be multiple of 4, according to problem definition 
result = zeros(M,N); %// preallocate and initiallize to zeros
result(1, randsample(1:N,N/2)) = 1; %// first row: half values set to one, half to zero
for m = 2:M
    result(m, :) = result(m-1, :); %// initiallize row m equal to row m-1
    result(m, randsample(find(result(m-1,:)), N/4)) = 0; %// change half of ones
    result(m, randsample(find(~result(m-1,:)), N/4)) = 1; %// change half of zeros
end

示例结果:

result = 
     0 1 0 1 1 0 0 0 0 1 0 1 1 0 1 1
     1 1 0 0 0 1 1 1 0 1 0 1 0 0 0 1
     1 0 0 0 1 0 0 1 0 1 1 0 1 1 0 1