MATLAB: How to generate two random binary images with remove overlapping

random imagerandom number generator

hi,
I am looking to creat two random binary images with remove overlapping. the second image should be random and not contact or overlape with first one.I have did this code:
clc; clear all;
A=[0 0 0 0 ;...
0 0 0 0 ;...
0 0 0 0 ;...
0 0 0 0 ;...
0 0 0 0];
N =2;
for k= 1:N
if k>1
(A(y0:y1,x0:x1)+1)==[];
if A>=0
X(k,:) = randi([1,min(size(A))],1,2);
Y(k,:) = randi([1,min(size(A))],1,2);
x0= min(X(k,:));
x1= max(X(k,:));
y0= min(Y(k,:));
y1= max(Y(k,:));
A(y0:y1,x0:x1)=1;
xyStart(k,:) = [y0,x0];
xyEnd(k,:) = [y1,x1];
end
else
X(k,:) = randi([1,min(size(A))],1,2);
Y(k,:) = randi([1,min(size(A))],1,2);
x0= min(X(k,:));
x1= max(X(k,:));
y0= min(Y(k,:));
y1= max(Y(k,:));
A(y0:y1,x0:x1)=1;
xyStart(k,:) = [y0,x0];
xyEnd(k,:) = [y1,x1];
end
end
this code work with first one but does not work with the seconed image, could you please help me?
one example of expect answer:(Note it are random)
A=[1 1 0 0 ;...
1 1 0 0 ;...
0 0 0 0 ;...
0 1 1 1 ;...
0 1 1 1];

Best Answer

OK, maybe I could understand your intention.
How about the following way?
nRow = 5;
nCol = 4;
while true
% Prepare a blank binary image
A = false(nRow,nCol);
% Put two rectangle randomly
for kk2 = 1:2
r = randi(nRow);
c = randi(nCol);
h = randi(nRow - r + 1);
w = randi(nCol - c + 1);
A(r:r+h-1,c:c+w-1) = true;
end
% If these two rectangles are separated, then stop the process
s = regionprops(A);
if numel(s) == 2
break;
end
end