Combination of 2 numbers in 7 rows

Hi!
I need to get all possible combinations of 1 and 0 in 7 rows, 128 different combinations. How can i get this in Matlab? I currently uisng Matlab 7.6.0

Answers (2)

DEC2BIN converts the doubles to chars, and the -'0' converts them back to doubles. Directly:
out = rem(floor((0:127)' * pow2(-6:0)), 2)
dec2bin(0:127)
Building on the comments I am going to edit my answer. The above command produces 128 rows of 7 element strings. Specifically it is a 128x7 matrix of class char. That was my interpretation of what you wanted. Andrei and Walter think you want a 128x7 matrix of class double. You can get this with
dec2bin(0:127)-'0'
You also might want a 128x7 matrix of class logical
logical(dec2bin(0:127)-'0')

3 Comments

dec2bin(0:127)+'0'-96
dec2bin(0:127)-'0'
@Andrei and Walter, I wonder want Per wants (char or double), although I think logical is probably better than double. If you put them as an answer, maybe we will find out.

This question is closed.

Asked:

on 29 Nov 2011

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!