0
votes

This code

 combinations = dec2base(0:power(2,N*M)-1,2) - '0'

generates all possible combinations of zeros and ones for a matrix of size N*M and stores all these combinations in a matrix called combinations. I need to know how it works , because i don't understand this code . Thank you

1
If you want to know what dec2base internally does, type open dec2base and see. Be careful not to modify anything. If what confuses you is some other part, please specify in your questionLuis Mendo
Thank you Luis, i know what dec2base () function do , it suppose to to converts an integer number to base d for example , but how this function is used to get all the possible combinations of and N*M matrix using this code ? In other words, what is this arguments do and what is "-'0'" ?Joo
No, this creates a 2^(M*N) x (M*N) matrix. The - '0' converts the char output of dec2base to numeric.beaker
Thank you beaker !Joo

1 Answers

3
votes

Consider M = 2, N = 3 as an example. Then power(2,N*M)-1 is 63, and 0:power(2,N*M)-1 is the vector [0 1 2 ... 63].

dec2base(..., 2) converts those 64 numbers into base 2, using chars '0' and '1' as "digits". Each result is in a row, left-padded with '0''s if needed. So it gives the 64×6 char matrix

000000
000001
000010
....
111110
111111

To convert those chars into numbers, subtract '0'. That gives 0 for '0' and 1 for '1', exploiting the fact that the ASCII codes for chars '0' and '1' are consecutive. So the final result is the numeric matrix

0     0     0     0     0     0
0     0     0     0     0     1
0     0     0     0     1     0
....
1     1     1     1     1     0
1     1     1     1     1     1