给定一个二元方阵。我想得到所有可能的二进制矩阵,它们d
相距汉明距离。
认为
A=[1 0 1;
0 1 1;
1 1 0].
那么相隔一 (d) 汉明距离的矩阵是
[0 0 1;
0 1 1;
1 1 0].
Matlab基础编码有什么帮助吗?
给定一个二元方阵。我想得到所有可能的二进制矩阵,它们d
相距汉明距离。
认为
A=[1 0 1;
0 1 1;
1 1 0].
那么相隔一 (d) 汉明距离的矩阵是
[0 0 1;
0 1 1;
1 1 0].
Matlab基础编码有什么帮助吗?
我希望我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 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
对于 big 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