Info

This question is closed. Reopen it to edit or answer.

Would you please help me to correct this code?

11 views (last 30 days)
Sherwin
Sherwin on 30 Sep 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi, I want to generate a 4*12 matrix that consists of 0 and 1 elements only that each row is divided into 2 sections with 6 elements. For example in row 1, in the first 6 elements, maximum number of consecutive 0 arrays should be 3, in the second 6 elements maximum number of consecutive 0 arrays should be less than or equal to 4 and so on(Max matrix in code). I've coded it but there are some parts I don't know how to code. Would you please help me with it? (Questions are asked on code in % comments.)
X = zeros(4,12);
Max = [3,4;4,5;3,5;2,4];
counter=0;
for i = 1:4
for j = 1:2
while z <= 6
X = round(rand(1,Max(i,j)));
if X(i:Max(i,j))==0 %is : right? I it to consider each element in that range.
counter = counter+1; % for each zero, a unit needs to be added to counter.
else continue;
if counter >= Max(i,j)
X(i,z+1) = 1; % I want it to set the value of the next immediate element after the last zero equal to 1.
counter = 0; % and restart the counter for the next 0 appearing.
else continue; % i want it to continue the loop exactly from last element with the value of 1
end
end
end
end
end

Answers (1)

James Tursa
James Tursa on 30 Sep 2016
Edited: James Tursa on 30 Sep 2016
Try this code (assumes spot_length is not too large):
nrows = 4; % Number of rows
ncols = 2; % Number of column spots
spot_length = 6; % Length of each column spot
X = zeros(nrows,ncols*spot_length); % Allocate result
Max0 = [3,4;4,5;3,5;2,4]; % Max number of consecutive 0's for each spot (should be nrows x ncols)
for m = 1:size(Max0,1)
for n = 1:size(Max0,2)
p = good_patterns(spot_length,Max0(m,n)); % Get allowable patterns for this spot
k = spot_length*n - 5; % Starting column number for this spot
X(m,k:k+spot_length-1) = p(randi(size(p,1)),:); % Pick a random allowable pattern
end
end
X = X - '0'; % Turn char array into double array
with this supporting function:
function p = good_patterns(n,z)
p = dec2bin(0:2^n-1); % All patterns of length n
f = false(size(p,1),1); % Flag vector for picking off allowable patterns
bad = repmat('0',1,z+1); % The bad pattern we want to avoid
m = numel(bad);
for k=1:size(p,2)-m+1 % Step through the starting column of p
f = f | all(bsxfun(@eq,p(:,k:k+m-1),bad),2); % Flag any bad patterns
end
p = p(~f,:); % Pick off the allowable (i.e., not bad) patterns
end
  3 Comments
Sherwin
Sherwin on 30 Sep 2016
Edited: Sherwin on 30 Sep 2016
and what if the matrix and the spot_length is large... like a 4*960 matrix that is divided into 60 elements instead of 6? can I just replace the values?
Sherwin
Sherwin on 1 Oct 2016
Edited: Sherwin on 1 Oct 2016
And I think there has been a misunderstanding. The matrix has only 0 and 1 elements they are not binary representations of anything. Something like round(rand(1,max(i,j)) might help. But I don't know enough.

This question is closed.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!