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

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.
Greg
Greg 2015년 12월 14일
Agreed. Thanks for getting back to me so quickly jgg. Much appreciated.

댓글을 달려면 로그인하십시오.

 채택된 답변

Andrei Bobrov
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

Greg
Greg 2015년 12월 14일
Thanks for your quick answer Andrei, much appreciated.
How we can generate the binary matrix of 0 , 1 whose row is 2^N and coloumn is N
DGM
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.
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')
A = 9x9 logical array
1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1
This all seems like a sound interpretation of the question, but the fact that this is just
A = eye(N)
A = 8x8
1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
... suggests that:
  • my interpretation was wrong
  • the task was not clearly described
  • the solution was truly trivial
or possibly all of the above.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2015년 12월 14일

댓글:

DGM
2024년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by