How can I generate a binary matrix of permutations
2 ビュー (過去 30 日間)
古いコメントを表示
Hi, I'd like to ask for help on generating a binary matrix of permutations given several variables:
The first being the number of "ones" per row (n). The second being the length of each row (m). For example, if I was asked to generate a binary matrix given n = 2 and m = 4 the matrix generated should look like the following:
0011
0101
0110
1001
1010
1100
I've been asked to prepare a list of all permutations to select n=23 items out of a list of m=30.
I first thought of generating a matrix of binary numbers incrementing by 1:
0000
0001
0010
0011...
Then going through each row and checking the sum of ones. If that sum equals n then store that row vector in another variable and build my desired matrix that way. This is not efficient though. I am now thinking of something along the lines of this pseudo-code:
for i = 1 to 2^30
x = convert i to binary
if (count of ones in x) = n then y = x
I hope the problem is correctly phrased and I apologies for the simplicity of this question or if it has previously been asked - I'm a complete novice both in Matlab and in using this forum. Your help would be much appreciated.
Greg
2 件のコメント
jgg
2015 年 12 月 14 日
I think it might be easier to do an interative loop.
Start with like (in your example) 1100
Then iterate backwards by "moving" the rightmost-1 to the right.
1100
1010
1001
0110
0101
0011
You should be able to do this by recursion; that's definitely going to be better than try to perform billions of operations.
採用された回答
Andrei Bobrov
2015 年 12 月 14 日
編集済み: Andrei Bobrov
2015 年 12 月 14 日
n = 2;
m = 4;
ii = nchoosek(1:m,n);
k = size(ii,1);
out = zeros(k,m);
out(sub2ind([k,m],(1:k)'*ones(1,n),ii)) = 1;
4 件のコメント
DGM
2023 年 12 月 27 日
Other than a binary array, it's unclear what you're asking for, or if it's even related to this thread.
Given that everyone in this thread has been inactive for at least a year, it's not likely they're going to respond. If you have a question, open a new thread and ask a question. Be clear about what you want. Provide an example of your expected inputs and outputs.
DGM
2024 年 4 月 6 日
Wow. I've had this tab open for over three months. I'm just going to take a wild stab at answering anyway.
I'm going to interpret this as to say that we want a binary, possibly logical array A of size NxN, where the column vector at column n is a binary representation of 2^n. For sake of plausible convention, I'm assuming a zero-based indexing. Each column vector is ordered LSB first.
N = 8; % the number of columns
A = rot90(dec2bin(2.^(0:N)) == '1')
This all seems like a sound interpretation of the question, but the fact that this is just
A = eye(N)
... suggests that:
- my interpretation was wrong
- the task was not clearly described
- the solution was truly trivial
or possibly all of the above.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!