我想生成(或计数)满足以下条件的所有可能的二进制矩阵。 令A为任意二进制矩阵4 * 4
A= [0 0 1 1] [0 0 1 1] [1 1 0 0] [1 1 0 0] [sum(row1) sum(r2) sum(r3) sum(r4) sum(column1) sum(c2) sum(c3) sum(c4)] condition: [2 2 2 2 2 2 2 2] 1)how many matrix satisfy above condition? 2)how can i generate them? answer 1 is:90 but i want a formula or algorithm , because i want to use it for 1024*1024 or upper and every arbitrary condition vector.
答案 0 :(得分:0)
蛮力方法:生成所有4x4二进制矩阵并测试其中哪些符合您的条件:
condition = [2 2 2 2 2 2 2 2]; %// desired conditions
matrices = reshape(dec2bin(0:2^16-1,16).'-'0', 4,4,[]); %'// all binary matrices
ind = all(bsxfun(@eq, [squeeze(sum(matrices,1)); squeeze(sum(matrices,2))],...
condition(:))); %// gives 1 for matrices that fulfill the condition, or else 0
result = matrices(:,:,ind); %// pick solution matrices
number = size(result,3); %// number of solution matrices
解决方案矩阵为result(.,:,1)
,result(.,:,2)
,......;并且number
是解决方案矩阵的数量。
你可以加快一点利用对称性。