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

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

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

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

第三行随机地将零分配给第二行中的0和1的50%,并将剩余的50%分配给0.

非随机化示例:

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
相关文章
相关标签/搜索