How to create all possible matrices of zeros and ones without having more than one '1' in the same column?

3 views (last 30 days)
I want to create all possible matrices of zeros and ones without having more than one '1' in the same column and then save each combination individually
For Example n=number of rows and m= number of column. So the possible combination number is C=n^m. Assuming n=2 and m=3 so C=8. And I need the output to be in a matrix form for each combination as shown below.
X1 = [1 1 1
0 0 0]
X2 = [0 0 0
1 1 1]
X3 = [1 0 0
0 1 1]
X4 = [0 1 1
1 0 0]
X5 = [1 1 0
0 0 1]
X6 = [0 0 1
1 1 0]
X7 = [1 0 1
0 1 0]
X8 = [0 1 0
1 0 1]
  2 Comments
Bruno Luong
Bruno Luong on 12 Jan 2023
Edited: Bruno Luong on 12 Jan 2023
The problem suggested by @Steven Lord is almost the same problem for exactly one 1 by column:
Simply generate for all possible (m+1) x n matrix with exactly one 1 by column; then thow away one row, as the last row.
m = 2;
n = 3;
c = cell(1,n);
[c{:}] = ndgrid(1:m+1); % change here
i = reshape(cat(n+1,c{:}),[],n);
[j,k] = meshgrid(1:n,1:size(i,1));
X = accumarray([i(:) j(:) k(:)],1);
X(end,:,:) = []; % new here
X
X =
X(:,:,1) = 1 1 1 0 0 0 X(:,:,2) = 0 1 1 1 0 0 X(:,:,3) = 0 1 1 0 0 0 X(:,:,4) = 1 0 1 0 1 0 X(:,:,5) = 0 0 1 1 1 0 X(:,:,6) = 0 0 1 0 1 0 X(:,:,7) = 1 0 1 0 0 0 X(:,:,8) = 0 0 1 1 0 0 X(:,:,9) = 0 0 1 0 0 0 X(:,:,10) = 1 1 0 0 0 1 X(:,:,11) = 0 1 0 1 0 1 X(:,:,12) = 0 1 0 0 0 1 X(:,:,13) = 1 0 0 0 1 1 X(:,:,14) = 0 0 0 1 1 1 X(:,:,15) = 0 0 0 0 1 1 X(:,:,16) = 1 0 0 0 0 1 X(:,:,17) = 0 0 0 1 0 1 X(:,:,18) = 0 0 0 0 0 1 X(:,:,19) = 1 1 0 0 0 0 X(:,:,20) = 0 1 0 1 0 0 X(:,:,21) = 0 1 0 0 0 0 X(:,:,22) = 1 0 0 0 1 0 X(:,:,23) = 0 0 0 1 1 0 X(:,:,24) = 0 0 0 0 1 0 X(:,:,25) = 1 0 0 0 0 0 X(:,:,26) = 0 0 0 1 0 0 X(:,:,27) = 0 0 0 0 0 0

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 12 Jan 2023
Edited: Bruno Luong on 12 Jan 2023
m = 2;
n = 3;
c = cell(1,n);
[c{:}] = ndgrid(1:m);
i = reshape(cat(n+1,c{:}),[],n);
[j,k] = meshgrid(1:n,1:size(i,1));
X = accumarray([i(:) j(:) k(:)],1)
X =
X(:,:,1) = 1 1 1 0 0 0 X(:,:,2) = 0 1 1 1 0 0 X(:,:,3) = 1 0 1 0 1 0 X(:,:,4) = 0 0 1 1 1 0 X(:,:,5) = 1 1 0 0 0 1 X(:,:,6) = 0 1 0 1 0 1 X(:,:,7) = 1 0 0 0 1 1 X(:,:,8) = 0 0 0 1 1 1

More Answers (1)

Jan
Jan on 12 Jan 2023
Edited: Jan on 12 Jan 2023
n = 3;
m = 4;
c = n^m;
X = cell(1, c); % List of outputs
v = n .^ (1-m:0); % Calculate expensive power operation once
for ic = 0:c - 1
index = rem(floor(ic .* v), n); % Column indices of 1s
XX = zeros(n, m);
XX(sub2ind([n, m], index + 1, 1:m)) = 1;
X{ic + 1} = XX;
end
X{1}, X{2}, X{end-1}, X{end}
ans = 3×4
1 1 1 1 0 0 0 0 0 0 0 0
ans = 3×4
1 1 1 0 0 0 0 1 0 0 0 0
ans = 3×4
0 0 0 0 0 0 0 1 1 1 1 0
ans = 3×4
0 0 0 0 0 0 0 0 1 1 1 1
This chooses m numbers from 1:n as column index of 1s in a nxm matrix of 0s.
  4 Comments

Sign in to comment.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!