简体   繁体   English

在MATLAB中生成所有可能的配置

[英]Generate all possible configurations in MATLAB

I have a 2D matrix A , which contains only binary values: 我有一个2D矩阵A ,它仅包含二进制值:

A = [0 0
     0 1
     1 0
     1 1];

I want to create a function which generates all possible configurations of the values in A . 我想创建一个函数,该函数生成A值的所有可能配置。 In this case, the word configuration corresponds to any possible combination of values within a row of the data (ie, pairs of columns, triplets, etc). 在这种情况下,字配置对应于数据行(即,成对的列,三元组等)内值的任何可能组合。 For example, in the simple case of the data provided above, i want the function to return: 例如,在上面提供的数据的简单情况下,我希望函数返回:

B = [ A(:,1)==1, ...
      A(:,2)==1, ...
      A(:,1)==0 & A(:,2)==0, ...
      A(:,1)==0 & A(:,2)==1, ...
      A(:,1)==1 & A(:,2)==0, ...
      A(:,1)==1 & A(:,2)==1];

B =

     0     0     1     0     0     0
     0     1     0     1     0     0
     1     0     0     0     1     0
     1     1     0     0     0     1

However, i would like the function to be able to handle matrices of any size. 但是,我希望该函数能够处理任何大小的矩阵。 In the case of a 3-column matrix, the resulting number of configurations is much larger: 对于3列矩阵,配置的结果要大得多:

A = [ 0 0 0
      0 0 1
      0 1 0
      0 1 1
      1 0 0
      1 0 1
      1 1 0
      1 1 1]

B = [A(:,1)==1, ...
     A(:,2)==1, ...
     A(:,3)==1, ...
     A(:,1)==0 & A(:,2)==0, ...
     A(:,1)==0 & A(:,2)==1, ...
     A(:,1)==0 & A(:,3)==0, ...
     A(:,1)==0 & A(:,3)==1, ...
     A(:,1)==1 & A(:,2)==0, ...
     A(:,1)==1 & A(:,2)==1, ...
     A(:,1)==1 & A(:,3)==0, ...
     A(:,1)==1 & A(:,3)==1, ...
     A(:,2)==0 & A(:,3)==0, ...
     A(:,2)==0 & A(:,3)==1, ...
     A(:,2)==1 & A(:,3)==0, ...
     A(:,2)==1 & A(:,3)==1, ...
     A(:,1)==0 & A(:,2)==0 & A(:,3)==0, ...
     A(:,1)==0 & A(:,2)==0 & A(:,3)==1, ...
     A(:,1)==0 & A(:,2)==1 & A(:,3)==0, ...
     A(:,1)==0 & A(:,2)==1 & A(:,3)==1, ...
     A(:,1)==1 & A(:,2)==0 & A(:,3)==0, ...
     A(:,1)==1 & A(:,2)==0 & A(:,3)==1, ...
     A(:,1)==1 & A(:,2)==1 & A(:,3)==0, ...
     A(:,1)==1 & A(:,2)==1 & A(:,3)==1]

This seems like a pretty challenging problem, so i'm wondering if the SO community has any ideas! 这似乎是一个非常具有挑战性的问题,所以我想知道SO社区是否有任何想法!

I use this [ugly] function currently. 我目前使用此[ugly]函数。 It relies on the the allcomb function from the MATLAB file exchange: 它依赖于MATLAB文件交换中的allcomb函数:

function [B] = allconfigs(A)

% some information about A
N = size(A,1);  
D = size(A,2);

% set up storage
B = A==1;

% iterate over levels of dimensionality (pairs, triplets, etc)
%   I==1 can be ignored, as it is equal to A.
%   I==(D-1) can be ignored, as it is an identity matrix of size N

for I = 2:(D-1)

%   get all possible values given dimensionality I
    possiblevalues  = cell(1,I);
    for j = 1:I
        possiblevalues{j} = [0 1];
    end
    possiblevalues = allcomb(possiblevalues{:});
    npossible = size(possiblevalues,1);

%   get all possible combinations of dimensions
    combinations = combnk(1:D,I);
    ncombs = size(combinations,1);

%   check if the data under each dimension combination matches each value possibility
    for J = 1:ncombs
        dimensions = A(:,combinations(J,:));

        for K = 1:npossible
            matches = dimensions == repmat(possiblevalues(K,:),[N,1]);
            matches = all(matches==1,2);

            B = cat(2,B,matches);
        end
    end
end

% if I is the full set of data, the matches are an identity matrix.
B = cat(2,B,eye(N));
return

That function returns the correct results (though note that the columns it produces are not in the same order that i typed out). 该函数返回正确的结果(尽管请注意,它产生的列与我键入的顺序不同)。 Its just so darn ugly. 它真是太丑了。 Does anyone know of something more elegant? 有人知道更优雅的东西吗?

I still got a problem understanding the pattern which you generate for B, nevertheless there are probably only one or two lines missing. 在理解您为B生成的模式时,我仍然遇到问题,但是可能只缺少一两行。 My idea to simplify is, to encode the combinations in [1,0,nan] where nan stands for don't care. 我要简化的想法是,将组合编码为[1,0,nan],其中nan表示无关紧要。 For example A(:,1)==1 & A(:,2)==0 would be represented as [1,0,nan] 例如, A(:,1)==1 & A(:,2)==0将表示为[1,0,nan]

This code builds up all combinations you want in B. As I did not fully understand the pattern, it generates all possible combinations: 这段代码在B中构建了您想要的所有组合。由于我不完全了解该模式,因此它会生成所有可能的组合:

c=size(A,2);
p={[0,1,nan]};
%1=colum is 1
%0=colum is 0
%nan= don't care
all_rows=allcomb(p{ones(c,1)});
%TODO filter out unwanted rows

Now it's simple, compare each row in all_rows with each colum of your input. 现在很简单,将all_rows中的每一行与输入的每个列进行比较。 Its true if both are equal or the reference is nan (don't care) 如果两者相等或引用为nan,则为true(不在乎)

B=true(size(A,1),size(all_rows,1));
for ix=1:size(all_rows,1)
    B(:,ix)=all(bsxfun(@(a,b)isnan(a)|eq(a,b),all_rows(ix,:),A),2);
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM