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

1 回表示 (過去 30 日間)
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 件のコメント
Steven Lord
Steven Lord 2023 年 1 月 12 日
You're missing several possibilities. One such example is
z = zeros(2, 3)
z = 2×3
0 0 0 0 0 0
This has no more than one 1 in the same column. Is this a valid matrix you want your function to generate?
Bruno Luong
Bruno Luong 2023 年 1 月 12 日
編集済み: Bruno Luong 2023 年 1 月 12 日
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

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2023 年 1 月 12 日
編集済み: Bruno Luong 2023 年 1 月 12 日
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

その他の回答 (1 件)

Jan
Jan 2023 年 1 月 12 日
編集済み: Jan 2023 年 1 月 12 日
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 件のコメント
Jan
Jan 2023 年 1 月 12 日
@Bruno Luong: You are right - this is instable. I've taken it from dec2bin (it was an m-file in former Matlab versions), but hier n=2 is less susceptible to rounding artifacts.
This is better to produce the column indices:
function I = PermRepOrder(n, m)
a = 1;
b = n^m;
I = zeros(b, m);
v = (1:n).';
for k = 1:m
b = b / n;
I(:, k) = repmat(repelem(v, b, 1), a, 1);
a = a * n;
end
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by