Matrix 0s and 1s If the assignment in the following lines is conditional on the previous line

I would like to create a matrix in MATLAB, where:

The first line consists of a random arrangement of 0s and 1s, separated evenly (i.e. 50-50).

The second line randomly assigns zeros 50% of 0s and 1s in the first line, and the rest 50%.

The third line randomly assigns zeros 50% 0 and 1 in the second line, and the rest - 50%.

Non-randomized example:

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 

Any suggestions?

+7
matlab psychtoolbox
source share
4 answers

A solution based on checking if there are more or less numbers than the median. As long as the number of columns checked is exactly equal, exactly half of the set of random doubles will be more than the median, and half will be less. This ensures that exactly 50% of the bits are flipped.

 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 
+7
source share

I would suggest a short bsxfun solution:

 %// 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 

First you need to create an undeveloped matrix, which can be done by comparing the matrix generated using meshgrid(1:n^2,1:n) with a row-dependent module. Finally, you just need to shuffle the columns.

+3
source share

If you have a Statistics Toolkit, you can do this very easily with 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:

 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 
+3
source share

Solution using 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 
+2
source share

All Articles