2015-10-20 31 views
1

所有可能的配置我有一個二維矩陣A,其中只包含二進制值:產生MATLAB

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

我想創建,其產生A值的所有可能的配置功能。在這種情況下,單詞配置對應於一行數據(即,列對,三元組等)內的值的任何可能的組合。例如,在以上提供的數據的簡單情況下,我想函數返回:

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 

然而,我想的功能能夠處理任意大小的矩陣。在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] 

這似乎是一個非常具有挑戰性的問題,所以我想知道如果SO社區有任何想法!

我目前使用這個[醜陋]功能。它依賴於從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 

該函數返回正確的結果(但要注意,它產生的列不在相同的順序我輸入了)。它太醜了。有誰知道更優雅的東西?

+0

我有一些理解模式的問題。爲什麼是'A(:1)== 0&A(:,2)== 0&A(:,3)== 0,'和A(:,1)== 0&A(:, 2)== 0'包含但不包含'A(:,1)== 0'? – Daniel

+0

「A」僅僅是一個例子還是總是枚舉所有二進制向量? – Daniel

+0

因爲'A'的值是0或1,所以不需要對'A(:,1)== 0'和'A(:,1)== 1'進行編碼,這就是爲什麼我跳過放在那裏。'A'只是一個例子 - 實際上所有的二進制向量可能不存在。 –

回答

0

我仍然有一個理解你爲B生成的模式的問題,但是可能只有一兩行缺失。我想簡化的方法是,將編碼的組合編碼爲[1,0,nan],其中nan代表不理會。例如A(:,1)==1 & A(:,2)==0將表示爲[1,0,nan]

該代碼構建了你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 

現在,它的簡單,比較all_rows中的每一行都與您輸入的每個列相關聯。它是真的,如果兩者相同或參考是南(不在意)

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