给定方形二进制矩阵。我希望得到所有可能的二进制矩阵,它们距离d
汉明距离。
假设
A=[1 0 1;
0 1 1;
1 1 0].
然后,一个(d)汉明距离的矩阵是
[0 0 1;
0 1 1;
1 1 0].
Matlab基本编码的任何帮助?
答案 0 :(得分: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)
因此,out
的每个3D切片都会为您提供3 x 3
矩阵,其间有1
汉明重量和A
。
看起来你有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