Вопрос

is there a simple command to do this:

M = 0 0 0
    0 0 1
    0 1 0
    0 1 1
    1 0 0
    1 0 1
    1 1 0
    1 1 1

i found this http://www.mathworks.co.kr/matlabcentral/fileexchange/7147-combn--4-3- but this isn't yet a thing in matlab

tried M = combn([0 1],3) bo i got Undefined function 'combn' for input arguments of type 'double'. in return

Это было полезно?

Решение

An easy approach is to generate all numbers from 0 to 2^n-1, where n is the number of rows, and convert them into binary:

n = 3;
result = dec2bin(0:2^n-1)-'0';

Or particularize this Q&A to your case.

Другие советы

You're looking for matlab's perms() function.

v0=[0 0 0]; 
v1=[1 0 0];
v2=[1 1 0];
v3=[1 1 1]; 
p1=perms(v1);
p2=perms(v2);
combs = cat(1,v0,p1,p2,v3);

This of course gives all permutations meaning that order matters (even for the zeros) so to get only the unique combinations you could then use:

combs=unique(combs,'rows');

Which gives:

combs =
     0     0     0
     0     0     1
     0     1     0
     0     1     1
     1     0     0
     1     0     1
     1     1     0
     1     1     1

As a side note if you downloaded the file from the file exchange make sure it is on your matlab path, or in your current directory and this will avoid the unknown function error. You can read about how matlab's path works here

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top