Matlab 我想生成(或计算)满足一定条件的所有可能的二进制矩阵
我想生成(或计数)满足以下条件的所有可能的二进制矩阵。 设A为任意二元矩阵4*4Matlab 我想生成(或计算)满足一定条件的所有可能的二进制矩阵,matlab,Matlab,我想生成(或计数)满足以下条件的所有可能的二进制矩阵。 设A为任意二元矩阵4*4 暴力法:生成所有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(matri
暴力法:生成所有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
解矩阵是结果(,:,1)
,结果(,:,2)
。。。;number
是解矩阵的个数
你可以利用对称性来加快速度。你有或想要公式/算法吗?谢谢路易斯·门多,但它不适用于20*20或更高的大尺寸,因为它应该生成非常非常。。。矩阵和…@user3375253您应该从一开始就指定需要使用的矩阵大小。不管怎么说,我想不出任何实质上不是暴力的方法,对不起
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