1

正方バイナリ行列が与えられます。dハミング距離で離れている可能性のあるすべてのバイナリ行列を取得したい。

仮定する

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

次に、1 (d) ハミング距離離れた行列は、

[0 0 1; 
 0 1 1; 
 1 1 0].

Matlab の基本コーディングの助けはありますか?

4

1 に答える 1

0

hamming weight与えられた文脈で正しい定義を得たことを願っています。その希望/仮定に基づいて、これはあなたが求めていたものかもしれません-

combs = dec2base(0:2^9-1,2,9)-'0'; %//'# Find all combinations
combs_3d = reshape(combs',3,3,[]); %//'# Reshape into a 3D array

%// Calculate the hamming weights between A and all combinations.
%// Choose the ones with hamming weights equal to `1`
out = combs_3d(:,:,sum(sum(abs(bsxfun(@minus,A,combs_3d)),2),1)==1)

したがって、 の各 3D スライスは、との間のハミング重みを持つ行列をout提供します。3 x 31A

あなたは9そのような行列を持っているようです -

out(:,:,1) =
     0     0     1
     0     1     1
     1     1     0
out(:,:,2) =
     1     0     1
     0     1     1
     0     1     0
out(:,:,3) =
     1     0     1
     0     0     1
     1     1     0
out(:,:,4) =
     1     0     1
     0     1     1
     1     0     0
out(:,:,5) =
     1     0     0
     0     1     1
     1     1     0
out(:,:,6) =
     1     0     1
     0     1     0
     1     1     0
out(:,:,7) =
     1     0     1
     0     1     1
     1     1     1
out(:,:,8) =
     1     1     1
     0     1     1
     1     1     0
out(:,:,9) =
     1     0     1
     1     1     1
     1     1     0

編集

大きなn場合は、ループを使用する必要があるようです-

n = size(A,1);
nsq = n^2;

A_col = A(:).';
out = zeros(n,n,nsq);
count = 1;
for k1 = 0:2^nsq-1
    match1 = dec2bin(k1,nsq)-'0';
    if sum(abs(match1-A_col))==1
        out(:,:,count) = reshape(match1,n,n);
        count = count + 1;
    end
end
于 2014-11-10T06:53:16.873 に答える