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



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

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

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

第三行将0随机分配给第二行中50%的0和1,将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

有什么建议吗?

基于检查数字是否大于或小于中值的解决方案。只要测试的列数是偶数,一组随机双打中正好有一半会比中位数大,一半会更小。这保证了正好有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)生成的矩阵与行相关模数进行比较来完成。最后,您只需要打乱列。

如果你有统计工具箱,你可以很容易地用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

使用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

相关内容

  • 没有找到相关文章

最新更新